izevaka / powersensor-home-assistant

6 stars 2 forks source link

Issue getting this working #1

Closed woopsicle closed 2 years ago

woopsicle commented 2 years ago

Hi,

First of all thank you for doing this! Really awesome to be able to integrate HA and PowerSensor data.

I haven't used NodeRed before and am having trouble getting an MQTT message through. I am not sure if its my lack of NodeRed knowledge or an issue with my PowerSensor.

First of all, the PowerSensor app shows my plug as "live data feed" as active and a "great" wireless signal. I have 1 plug and 2 sensors (meter box and one for solar).

I imported the NodeRed json, configured the IP address of the plug (192.168.1.63) and also configured the nodered global mqtt settings to the existing mqtt broker i have (used for zigbee2mqtt mainly).

I deleted the secondary plug nodes (connect and listen) in the flow.

In nodered, with tracing turned on I can see this in the logs:

14 May 15:55:41 - [info] [udp in:Listen to power plug #1] udp listener at 0.0.0.0:6788 14 May 15:55:41 - [info] [server:Home Assistant] Connecting to http://supervisor/core 14 May 15:55:41 - [info] [mqtt-broker:1dc4d152.a977d7] Connected to broker: mqtt://192.168.1.131:1883 14 May 15:55:41 - [info] [server:Home Assistant] Connected to http://supervisor/core 14 May 15:55:41 - [debug] [server:Home Assistant] States Loaded 14 May 15:55:41 - [debug] [server:Home Assistant] HA State: running 14 May 15:55:41 - [debug] [server:Home Assistant] Services Loaded 14 May 15:55:41 - [info] [udp out:Connect to power plug #1] udp re-use socket: 6788 -> 192.168.1.63:49476 14 May 15:55:41 - [debug] [inject:Start powersensor capture] repeat = 100000 14 May 15:55:45 - [trace] comms.close NJ08ojPVpPF3aTT1ypBhQt2igA+PB3QQF7QhAUedUJo= 14 May 15:57:03 - [trace] comms.open do/WeZzkGhgptJSw6lV1PcuQgAtcxTOB21nrVqkXyT8= 14 May 15:57:13 - [trace] comms.close do/WeZzkGhgptJSw6lV1PcuQgAtcxTOB21nrVqkXyT8=

It feels like this should be working but there are no messages on the mqtt broker when i "start listening" to # (all messages). I also use an mqtt app (mqtt explorer) to see all mqtt messages in a more structured way, and there is nothing there.

Also, the "comms.open" and "comms.close" messages dont really appear frequently like what i expected.

I tried to use the capture-sensor.mjs to ensure the plug connection is there and UDP messages are working, but not getting any meaningful output if i leave it for a while:

r$ node capture-sensor.mjs --raw 192.168.1.63 Scheduled resubscribe Scheduled resubscribe Scheduled resubscribe Scheduled resubscribe Scheduled resubscribe Scheduled resubscribe Scheduled resubscribe Scheduled resubscribe

So i am not sure if its an issue with the flow config, my mqtt settings or the plug itself.

I used wireshark to inspect the traffic, this is what i see after running for 20 mins (filtered on ipv4 traffic with source or destination as the IP of my plug): No. Time Source Destination Protocol Length Info 914 -1515.089677 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 2526 -1415.092284 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 3929 -1315.092920 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 15168 -1215.094326 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 16652 -1115.091741 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 18175 -1015.089620 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 19560 -915.087702 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 21011 -815.088554 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 23036 -715.083356 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 24870 -615.080537 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 38394 -515.082067 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 40237 -415.079549 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 42145 -315.077011 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 43812 -215.078230 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 45567 -115.077807 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 47338 -15.076374 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14 38780 -497.041847 192.168.1.63 239.255.193.68 UDP 72 52972 → 49476 Len=30 49404 84.923106 192.168.1.210 192.168.1.63 UDP 56 49429 → 49476 Len=14

Its mostly the sensor (only one for some reason) connecting to the plug with "subscribe 180" messages (source port is 49429, destination port is 49476). The plug connects to an external IP only a couple times in 20 mins, and just sends "calibrate until" messages (source port 52972, destination port 49476). Not sure if that is normal?

I tried looking in wireshark for anything happening over port 6788, but nothing is found.

Any assistance would be appreciated! And sorry if I am missing something really stupid.

izevaka commented 2 years ago

@woopsicle You deserve a virtual hug!

I've been battling the same issue, but manifesting itself in a different way for the last 4 days, after I got a new power plug. I haven't tried the capture-sensor.mjs script myself and after I did, I was able to reproduce the error and find the fix.

Here is what happened. There must have been a firmware change since I published this repo to require a newline after the UDP subscribe(###) command. The plug with f/w version 4294 does not need it, whereas 4918 does not stream data unless there is a newline in the subscribe command.

Should be fixed now, please let me know if it works for you 😀

woopsicle commented 2 years ago

Wow thats awesome. Almost there....

So now the capture-sensor.mjs definitely works, instantly starts sending me data! I am still having trouble with the node-red part, not resulting in any mqtt messages..

I imported the new json, removed the plug #2 nodes and deployed.

15 May 14:43:14 - [debug] red/nodes/flows.start : starting flow : 5ade710a50eb7048 15 May 14:43:14 - [trace] [flow:global] start flow [global] 15 May 14:43:14 - [trace] [flow:global] ------------------|--------------|----------------- 15 May 14:43:14 - [trace] [flow:global] id | type | alias 15 May 14:43:14 - [trace] [flow:global] ------------------|--------------|----------------- 15 May 14:43:14 - [trace] [flow:global] dc049ea2.cde4e | server | 15 May 14:43:14 - [trace] [flow:global] 1dc4d152.a977d7 | mqtt-broker | 15 May 14:43:14 - [trace] [flow:global] ------------------|--------------|----------------- 15 May 14:43:14 - [trace] [flow:5ade710a50eb7048] start flow [5ade710a50eb7048] 15 May 14:43:14 - [trace] [flow:5ade710a50eb7048] ------------------|--------------|----------------- 15 May 14:43:14 - [trace] [flow:5ade710a50eb7048] id | type | alias 15 May 14:43:14 - [trace] [flow:5ade710a50eb7048] ------------------|--------------|----------------- 15 May 14:43:14 - [trace] [flow:5ade710a50eb7048] 6599f870aa6af0e6 | udp in | 15 May 14:43:14 - [trace] [flow:5ade710a50eb7048] 34f5915a315aa6ef | udp out | 15 May 14:43:14 - [trace] [flow:5ade710a50eb7048] f06526e7bf7c3ddb | inject | 15 May 14:43:14 - [trace] [flow:5ade710a50eb7048] 70e4890b16af0397 | json | 15 May 14:43:14 - [trace] [flow:5ade710a50eb7048] bde9364a6c244357 | mqtt out | 15 May 14:43:14 - [trace] [flow:5ade710a50eb7048] db06fbd7ca4f2fd0 | function | 15 May 14:43:14 - [trace] [flow:5ade710a50eb7048] ------------------|--------------|----------------- 15 May 14:43:14 - [trace] runtime event: {"id":"runtime-state","retain":true} 15 May 14:43:14 - [info] Started flows 15 May 14:43:14 - [trace] runtime event: {"id":"runtime-deploy","payload":{"revision":"76b3a4d669d1db78e46a2d79e088381d"},"retain":true} 15 May 14:43:14 - [info] [udp in:Listen to power plug #1] udp listener at 0.0.0.0:6788 15 May 14:43:14 - [info] [server:Home Assistant] Connecting to http://supervisor/core 15 May 14:43:14 - [info] [udp out:Connect to power plug #1] udp re-use socket: 6788 -> 192.168.1.63:49476 15 May 14:43:14 - [debug] [inject:Start powersensor capture] repeat = 100000 15 May 14:43:15 - [info] [mqtt-broker:1dc4d152.a977d7] Connected to broker: mqtt://192.168.1.131:1883 15 May 14:43:15 - [info] [server:Home Assistant] Connected to http://supervisor/core 15 May 14:43:15 - [debug] [server:Home Assistant] States Loaded 15 May 14:43:15 - [debug] [server:Home Assistant] Services Loaded 15 May 14:43:15 - [debug] [server:Home Assistant] HA State: running 15 May 14:43:25 - [trace] comms.close IB00QPwZDvMcdGesIHiWCcZPvZeq/l7I/Mkxaql+uLs=

But no messages when listening to # on the mqtt broker. I added some debug nodes, can see the "subscribe (180)" messages only (every 100 seconds), the rest of the ndoes (e.g. listen to plug #1 node) don't seem to trigger any debug output which makes me thing they arent being run.

I did another test flow in node-red, just a basic one with inject + sendmqtt nodes to ensure the mqtt broker is configured properly in node-red, and confirmed it sent the dummy messages fine and i could see then when listening to the broker.

Have attached some screenshots in case you see anything I have done wrong? Screen Shot 2022-05-15 at 2 46 31 pm Screen Shot 2022-05-15 at 2 48 29 pm Screen Shot 2022-05-15 at 2 48 36 pm Screen Shot 2022-05-15 at 2 49 19 pm .

izevaka commented 2 years ago

I buggered up the node-red flow and didn't commit the change to the node type. Should be fixed in dc4ab4c. The payload in the inject node should be JSONata like this:

Screen Shot 2022-05-15 at 5 34 49 pm

woopsicle commented 2 years ago

Working!!!!

Thank you so much :) Pls add a donation thing as I will donate.

I will next work on the sensors part and getting working in the energy dashboard (I am still a little confused how point in time power usage figures which this produces, can work in the energy dashboard which expects daily increasing totals, but I will think about that more another day). Also not sure how it will work out the amount of solar being used.

I can see the solar sensor might send slightly different info (unit: "I") as well. But my inverter is broken right now anyway so will share better output once I can make sense of the messages.

Made my day! Thank you! Thank you!

izevaka commented 2 years ago

Glad it's working for you 👍

Re energy - it's a bit of a mess, but I managed to get it to work with the sensors that I outlined in the README. In your case it might be more complex as you have a solar sensor as well. I haven't been able to reverse engineer the different power units, I suspect that the non-Watt units are something to do with the calibration "special sauce". The unit "I" must be unique to the solar sensor, I haven't seen that in my set-up.

The javascript bit that handles that is not particularly robust, so it will just let "I" units through. I found that the "U" units are about x19 at my place, so I added that fudge factor 🤷‍♂️ I probably should have explicitly accepted those, rather than assume that it's going to be either "W" or "U".

Screen Shot 2022-05-15 at 6 05 06 pm

Perhaps consider only accepting power readings that are in "W" units or add sensors to monitor the "I" readings to figure out their behaviour.

Feel free to donate to support the people of Ukraine during this time. The website https://standforukraine.com lists many trustworthy organisations that you can donate to, including many civilian and humanitarian outfits.

woopsicle commented 2 years ago

Just FYI, an update on this with the solar sensor.

The mains sensor returns a negative value when feeding into the grid, and a positive value when drawing from the grid.

The solar sensor returns a negative value when generating power.

I thought I had set up all the sensors properly and utility meter (in HA), but just realised I must have stuffed up something and it's doing my head in trying to work it out!

I will post on here if I figure it out so you can add to the docs (not sure I can navigate the pull request stuff).

izevaka commented 2 years ago

Very interesting. Out of interest, does the Powersensor app work correctly and produce the expected data in terms of generation, consumption from the grid, and export to the grid?

I am not sure what Home Assistant is expecting for the two sensors. To me, it makes sense that the solar sensor is always positive and that the mains sensor changes sign depending on whether or not there is net power consumption or net export. I would experiment by turning the solar sensor into an absolute value and see how that works with HA.

On Mon, May 30, 2022 at 2:30 PM woopsicle @.***> wrote:

Just FYI, an update on this with the solar sensor.

The mains sensor returns a negative value when feeding into the grid, and a positive value when drawing from the grid.

The solar sensor returns a negative value when generating power.

I thought I had set up all the sensors properly and utility meter, but just realised I must have stuffed up something and it's doing my head in trying to work it out!

I will post on here if I figure it out so you can add to the docs (not sure I can navigate the pull request stuff).

— Reply to this email directly, view it on GitHub https://github.com/izevaka/powersensor-home-assistant/issues/1#issuecomment-1140684817, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAC53L7G4M3XJS2KB4W5KKLVMQ76FANCNFSM5V5HXP2A . You are receiving this because you modified the open/close state.Message ID: @.***>

woopsicle commented 2 years ago

Yes the powersensor app works well...accurate to within 5% of my electricity provider and solar system reporting.

I am just using my Goodwe inverter as the input for the energy dashboard solar production. Not bothering with the powersensor reported generation details since it's pretty damn close.

I have some template sensors to only track positive main sensor values as grid usage, and to take the negative values as grid feed in (and convert to a positive number). Then doing reimann sum over each of those, then into a utility meter so it resets every day. But yeah, doing my head in as not quite right.

I will get it eventually.

woopsicle commented 2 years ago

Managed to get this accurate now, was missing the "method: left" for the Reimann integration.

In case anyone is interested.

sensor:  
  #note the way you do mqtt sensors will change in 2022.6 onwards
  - platform: mqtt
    state_topic: home/energy/sensor/<yoursensorid>/power
    name: main_powersensor_sensor
    device_class: power
    unit_of_measurement: W

 #do logic to interpret negative values as feed to grid, and positive values as draw from grid
  - platform: template
    sensors:
      house_energy_negative:
        unique_id: house_energy_negative
        unit_of_measurement: 'W'
        device_class: Power
        value_template: >
          {% if states('sensor.main_powersensor_sensor') | int > 0 %}
            0
          {% else -%}
            {{ (states('sensor.main_powersensor_sensor') | float) | abs }}  
          {% endif %}      

      house_energy_positive:
        unique_id: house_energy_positive
        unit_of_measurement: 'W'
        device_class: Power
        value_template: >
          {% if states('sensor.main_powersensor_sensor') | int > 0 %}
            {{ states('sensor.main_powersensor_sensor') }}
          {% else -%}
            0
          {% endif %}

#do reimann sum over them
  - platform: integration
    source: sensor.house_energy_negative
    name: energy_sent_back_to_grid
    unit_prefix: k
    round: 2
    method: left

  - platform: integration
    source: sensor.house_energy_positive
    name: energy_spent_energy_positive
    unit_prefix: k
    round: 2
    method: left

#this only adds total_increasing state_class to the reimann sum'd values - this probably can be done without a new sensor needing to be created.
template:
  - sensor:  
    - name: powersensor_template_fromgrid
      state: "{{ (states('sensor.energy_spent_energy_positive')) }}"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing
    - name: powersensor_template_togrid
      state: "{{ (states('sensor.energy_sent_back_to_grid')) }}"
      unit_of_measurement: "kWh"
      device_class: energy
      state_class: total_increasing

then just create 2x utility meters (through GUI or via yaml) which reference powersensor_template_togrid and powersensor_template_fromgrid with daily reset.

Then whack those two utility meters into your energy dashboard (along with your normal solar generation sensor). I have found its very close to what the powersensor app gives you. Within 0.2 of a kwh total.

I use my solar inverter generation integration with HA for completing the energy dashbaord, rather than the Powersensor one, but you could augment the above to use the powersensor solar generation value if you wanted to for some reason. You would just need to do the absolute value again to change the negative number the powersensor puts out to a positive and do a reimann sum again.

Maybe it will help someone.