I want to incorporate the output of a script in HA. The script provides a value for PV
Power
Daily energy
Total energy all time
every 5 seconds.
I have the opportunity to POST these values using curl every 5 seconds to HA. How do I set op HA properly so that it properly interprets this sensor as an energy meter?
I upload the “power” value using this command:
curl -X POST -H “Authorization: Bearer ##MYTOKEN##”
-H “Content-Type: application/json”
-d ‘{“state”: “500”, “attributes”: {“unit_of_measurement”: “W”, “friendly_name”: “Solar-power”}}’ http://192.168.2.120:8123/api/states/sensor.solar_power
500 is an example. The script fills in the correct value. How do I set up HA properly to have a PV sensor that I can use in the Energy Dash?
What you want is to set up a webhook in HA. I would not go the API route because you need a sensor you can set, which does not have a way to set the right state_class.
You create a webhook by creating an automation or by creating a trigger based template sensor using a webhook trigger. Usually I go for the latter, the principle is the same.
The sensors need the right device class, state class and unit for whatever type of sensor you are setting up. State class will need to be total_increasing for energy.
Interesting. I’l take a look. It is a bit difficult to understand what they mean with the different terms. I will figure it out. but when the webhook is in place and I upload some data: how do I connect that to a sensor with correct device and state class? Another thought: the daily and total energy values are total current values as is. It should not be added to previous values. Will that work with total_increasing?
The webhook trigger makes it so HA listens for messaged. It triggers when the data comes in. A trigger based template sensor uses the trigger to store the data in template sensors. They look a lot like the automation examples in the webhook documentation. But instead of actions you define the sensors beneath it, using templates to extract the data.
template:
- triggers:
- trigger: webhook
webhook_id: abacadabra
sensor:
- name: "My PV Power"
state: "{{ trigger.json.pvp }}"
unit_of_measurement: W
device_class: power
state_class: measurement
- name: "My PV Energy daily"
state: "{{ trigger.json.pved }}"
unit_of_measurement: Wh
device_class: energy
state_class: total_increasing
- name: "My PV Energy total"
state: "{{ trigger.json.pvet }}"
unit_of_measurement: Wh
device_class: energy
state_class: total_increasing
In the Energy dash I can select these as energy sources. Current Power will show in the graph. Energy production however, remains zero.
What could be wrong? I do see the updated values in the sensor overview
Also: after a restart of HA the values are unknown. How can I store these?
Also2: Does the energy dash require daily production or total production?
Values returning to zero is strange, Template sensors restore at startup for type sensor and binary_sensor.
I can’t tell why values remain 0 either without knowing what you send. Are you sure you are looking at the right entity? Hve you checked developer tools if there maybe are multiple entites with similar names?