Jump to content

Welcome to Smart Home Forum by FIBARO

Dear Guest,

 

as you can notice parts of Smart Home Forum by FIBARO is not available for you. You have to register in order to view all content and post in our community. Don't worry! Registration is a simple free process that requires minimal information for you to sign up. Become a part of of Smart Home Forum by FIBARO by creating an account.

 

As a member you can:

  •     Start new topics and reply to others
  •     Follow topics and users to get email updates
  •     Get your own profile page and make new friends
  •     Send personal messages
  •     ... and learn a lot about our system!

 

Regards,

Smart Home Forum by FIBARO Team


  • 0

Energy manager connected to a smart meter, Values in a VD


Mr Jack

Question

Just started with the HC2 and for sure completly new to LUA.
I already had an Iungo Energy manager to get the usage on electricity and gas from the smart meter, and also the water usage.
At this stage I managed to get a reponse from the energy manager, but didn't succeed to get the values for use in a virtual device.
 
I hope someone can give me a suggestion to put me in the right direction on how to get the values from the response.

Please login or register to see this code.

Response received is:

 

[DEBUG] 15:07:30: 200
[DEBUG] 15:07:30: 0

[DEBUG] 15:07:30: ENCODE RESULT :"{\"ok\":true,\"type\":\"response\",\"time\":0.001937499968335,\"rv\":{\"propsval\":[{\"id\":\"name\",\"value\":\"Smart meter\"},{\"id\":\"usage\",\"value\":362},{\"id\":\"T1\",\"value\":4113.167},{\"id\":\"T2\",\"value\":2884.556},{\"id\":\"-T1\",\"value\":0},{\"id\":\"-T2\",\"value\":0},{\"id\":\"L1I\",\"value\":1},{\"id\":\"L1Pimp\",\"value\":0.362},{\"id\":\"L1Pexp\",\"value\":0},{\"id\":\"c_tariff\",\"value\":2},{\"id\":\"gas_usage\",\"value\":0},{\"id\":\"gas\",\"value\":1681.49},{\"id\":\"Cost-T1\",\"value\":0.2166},{\"id\":\"Cost-T2\",\"value\":0.2166},{\"id\":\"Cost-nT1\",\"value\":0},{\"id\":\"Cost-nT2\",\"value\":0},{\"id\":\"Cost-gas\",\"value\":0.597},{\"id\":\"Client-blob\",\"value\":\"\"},{\"id\":\"available\",\"value\":true}]},\"systime\":1456149950,\"seq\":1,\"error\":false}"

 
Link to comment
Share on other sites

Recommended Posts

  • 2
  • Inquirer
  • Version 1.3.

     

    Added:

    Total energy usages from the Smart Meter, minus the energy values from all z-wave devices with power metering, to get the total of the remaining non measured power consumption.

    This calculated value is added to "power" of an unused relay input of a double fibaro switch. 

    Now it is shown in the energy panel and the toal enegry usages without counting values double.

    Please login or register to see this code.

    Please login or register to see this attachment.

    Please login or register to see this attachment.

    Please login or register to see this attachment.

    • Thanks 1
    Link to comment
    Share on other sites

    • 0

    I believe you need to decode the json string datasm.. and then you can use the table. For example to get usage value (362) and T1 value (4113.167) in your example:

    Please login or register to see this code.

    Have tested that on my HC2 and it works. See attached file

    Just copy above lines at end of your code.

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Thx Cag014,
     
    Copied the code, and get the response as in the first post, but get an error
    [ERROR] 20:09:42: line 14: attempt to index field 'rv' (a nil value)
     
     
    I saw that you entered your test code in a scene instead of a virtual device.
    Tried that also, but in the scene I get the error 
    [DEBUG] 20:14:20: line 7: attempt to index global 'Net' (a nil value)

     

     

     

    Any suggestions?

     

    Jack

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

    • 0

    OK, so here an answers to your questions:

     

    1. Net.FHttp function doesn't supported in scenes, in VDs only. 

    As you can see I dont have any Net.FHttp query in my code.I just defined datasm with your string.

    If you need to use scene, you can try to use  net.HTTPClient() instead (should be the same if written correctly).

    For example:

    Please login or register to see this code.

    2. The rv is not a field, this is an array of values more like sub-table which contents more tables (like propsval).

    Please refer to 

    Please login or register to see this link.

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Thanks cag014,

     

    Got it 

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

    At least got the basics of what I need.

    Now it's time to complete the VD

     

    Please login or register to see this attachment.

     

     

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Actual status of the VD

    Please login or register to see this code.

    Please login or register to see this attachment.

    Please login or register to see this attachment.

    Link to comment
    Share on other sites

    • 0

    Looking good.. well done

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Strange behaviour of the virtual device that I made.

    It stops exactly after 34 minutes.

    When I save it, it starts again and after 34 minutes it stops.

    Tested it 5 times this evening.

     

    Somebody any idea what could cause this?

    Link to comment
    Share on other sites

    • 0

    What do you mean by stops every 34 minutes? What exactly happens?

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • The LUA script stops.

    Normaly some of the values in the VD from the smartmeter change every 4 seconds, but after 34 minutes they do not change anymore.

    Let the debug run, and it prints the usage value every 4 seconds in the debug screen, but after 34 minutes it stops.

    Test3

    first value at 20:42:31

    Last vakue at 21:16:33

    Test4

    first value at 21:26:49

    Last vakue at 20:00:48

    Test5

    first value at 22:08:21

    Last vakue at 22:42:22

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Solved.
    Removing the below, did the job.

    --[[
    %% properties
    %% globals
    --]]

     

    Post #6 updated

     

    update 28-2-2016 19.04

    Not solved. Stopped again.

    Please login or register to see this image.

    /emoticons/default_icon_sad.gif" alt=":-(" />

    Link to comment
    Share on other sites

    • 0

    ​to avoid issues with main loop bug (not sure, is it still there?) try following:

    1. put everything into a VD button section instead of main loop
    2. create scene, which clicks the VD button every xxx seconds

    when you don't like the button in your VD, create two VDs, one hidden with button and one only with labels

    Please login or register to see this image.

    /emoticons/default_smile.png" alt=":)" srcset="https://forum.fibaro.com/uploads/emoticons/[email protected] 2x" width="20" height="20" />

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Hi Tinman,

    Thanks for the suggestion. I will keep it in mind, but first I will try to find the reason why this script in the main loop stops.

    I have a simular VD showing the temperature of two temperature sensors, and the calculated delta T of the two sensors. This one is running for more then a month now.

    The only difference is that this VD for the energy meter is using API, and has the fibaro:sleep

     

    At this moment the debug is running to see when it stops.

    At the first test, earlier this evening, stopped after almost 1 hour and 8 minutes, which is twice 34 minutes.

     

    Keep you posted

    Link to comment
    Share on other sites

    • 0

    Solved.

    Removing the below, did the job.

    --[[

    %% properties

    %% globals

    --]]

     

    Post #6 updated

     

    update 28-2-2016 19.04

    Not solved. Stopped again.

    Please login or register to see this image.

    /emoticons/default_icon_sad.gif" alt=":-(" />

    Have you tried calling your json repot only every 10 – 60 seconds as this may be a traffic error stopping the code due to frequency.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • Have you tried calling your json repot only every 10 – 60 seconds as this may be a traffic error stopping the code due to frequency.

    Hi John,

     

    Tested this during the day, but wasn't able to keep an eye on it.

    Sleep of 10 seconds let the script run between one and two hours.

    Sleep of 30 seconds kept the script running for approximately 4 hours.

    Can try to use 60 second, but then it get's more or less useless to measure the energy usage.

    Plan was to send the "usage" value to the log of an unused switch.

     

    Jack

    Link to comment
    Share on other sites

    • 0

    Hi Jack

    As you have two calls to the same equipment I would try putting 5 second sleep between the two and one at the end so you are only calling your smartmeter/watermeter every 5 seconds.

    Link to comment
    Share on other sites

    • 0

    when i built my own virtual device for energy reading, i remember it stopped after few minutes...

    sadly, i don't remember what i did to correct it

     

    looking at my code and comparing it to yours, i can see that i put the Net.Fhttp statement in an if clause, to avoid opening sockets continuosly:

    Please login or register to see this code.

    Link to comment
    Share on other sites

    • 0
    Jack I would try this code:
     
     
    setTimeout(function()
          --SMARTMETER 538d72d9
          local smartmeter= Net.FHttp('192.168.0.15');
          response,status,errorCode = smartmeter:POST("/iungo/api_request/", '{"seq":1,"method":"object_list_props_values","arguments":{"oid":"538d72d9"}}')
          local jTe = json.decode(response)
                usage = jTe.rv.propsval[2].value
                T1=jTe.rv.propsval[3].value
                T2=jTe.rv.propsval[4].value
                L1I=jTe.rv.propsval[7].value
                gas_usage=jTe.rv.propsval[11].value
                gas=jTe.rv.propsval[12].value
     
          setTimeout(function()
                --WATERMETER 0908f9a8
                --local watermeter= Net.FHttp('192.168.0.15');
                --response,status,errorCode = watermeter:POST("/iungo/api_request/", '{"seq":1,"method":"object_list_props_values","arguments":{"oid":"0908f9a8"}}')
                response,status,errorCode = smartmeter:POST("/iungo/api_request/", '{"seq":1,"method":"object_list_props_values","arguments":{"oid":"0908f9a8"}}')
                local jTw = json.decode(response)
                      flow=jTw.rv.propsval[1].value
                      pulstotal=jTw.rv.propsval[2].value
                      kfact=jTw.rv.propsval[3].value
                      wused=pulstotal/kfact
           
                  -- deviceid 99 Iungo VD
                 fibaro:call(99, "setProperty", "ui.Label1.value", usage.." Watt")
                 fibaro:call(99, "setProperty", "ui.Label2.value", T1.." kWh")
                 fibaro:call(99, "setProperty", "ui.Label3.value", T2.." kWh")
                 fibaro:call(99, "setProperty", "ui.Label4.value", L1I.." Amp")
                 fibaro:call(99, "setProperty", "ui.Label5.value", gas_usage.." m3")
                 fibaro:call(99, "setProperty", "ui.Label6.value", gas.." m3")
                 fibaro:call(99, "setProperty", "ui.Label7.value", flow.." liter/min")
                 fibaro:call(99, "setProperty", "ui.Label8.value", wused.." m3")
                 fibaro:log("T1n : "..T1.." kWh - T2d : "..T2.." kWh - Gas :"..gas.." m3");
           end, 5000)
    end, 5000)
    Link to comment
    Share on other sites

    • 0

    I just curious why you're using POST method ? As I understand you're trying to get data from the source (meter).

    It might be that the HTTP stuck somewhere.... did you try to use GET?

     

    There is no sense that VD stops after certain amount of time.... please add fibaro:debug() function in few places to get better picture where the code stops.

    Link to comment
    Share on other sites

    • 0
  • Inquirer
  • I just curious why you're using POST method ? As I understand you're trying to get data from the source (meter).

    It might be that the HTTP stuck somewhere.... did you try to use GET?

     

    There is no sense that VD stops after certain amount of time.... please add fibaro:debug() function in few places to get better picture where the code stops.

    Started with GET, but couldn't get it working. POST did the job.

    When I replace POST by GET then I get the following error

    [ERROR] 19:52:22: line :GET expected 2..2 args, got 3

    Link to comment
    Share on other sites

    Join the conversation

    You can post now and register later. If you have an account, sign in now to post with your account.

    Guest
    Answer this question...

    ×   Pasted as rich text.   Paste as plain text instead

      Only 75 emoji are allowed.

    ×   Your link has been automatically embedded.   Display as a link instead

    ×   Your previous content has been restored.   Clear editor

    ×   You cannot paste images directly. Upload or insert images from URL.

    ×
    ×
    • Create New...