When I reboot HA, sensor.mqttenery is in unavailable or unknown state. This sensor is used by energy configuration, after reboot the data comes to virtual sensor, and utility meter takes full consumption value.
I tried to add the “availability” to avoid this, but the problem remains…
How could I solve?
Have you tried a platform: mqtt sensor with a value_template? Unclear what value the template sensor offers here.
I suspect the issue is the way the template is written. It requires the trigger to trigger [/sic] and until it does so after an HA restart, there will be no data.
The availability will work as it will make the sensor unknown during startup and shutdown. When a sensor is unknown, it has no value. The only way that’s adding a full value is if the state is zero on startup and then jumps.
“messaggio” is an array with the value of power, energy and current. the template creates sensor.mqttenergy from the array. the array is sent each 30s.
I’m trying to solve this problem:
they suggest to use “availability” but I don’t know how implement it correctly…
The challenge you see with availability is the same root cause as the state; there is no value on an HA restart until the trigger is triggered. Hence the question to get you thinking why you are using a trigger when a trigger is the cause of the behavior. There are other mqtt sensors.
With an MQTT Sensor, if the the topic has a payload published as a retained message, the MQTT Sensor will always report the payload’s value, even on startup (i.e. it won’t be unknown).
Yes, It is correct the problem is the same like this post (and you have suggested the solution):
And you have replied: " Implement availability in your template sensors, based on whatever fits you.
If the availability is false, the template value will be ignored."
and now, I’d like to know how implement “availability”
See the response by taras.
As the template is only triggered by a message, the availability won’t be updated before a message comes in. Chicken, egg, …
They’re Trigger-based Template Sensors employing an MQTT Trigger.
The two concepts are different.
I guess that user overlooked to consider how their suggestion, to use a Trigger-based Template Sensor with an MQTT Trigger, behaves on startup.
FWIW, converting what you have into MQTT Sensors is trivial. Do you know if the payload published to the topic is a retained message? You can check by using an MQTT client like MQTT Explorer.
Personally, I’d go with @123’s suggestion and add them all as mqtt sensors. Using anchors it would be pretty simple as the value_template would be the only thing that changes if you set up the other fields properly.
The templates I posted in the example are probably wrong. I would need to see a sample of the actual payload in order to create the correct template. Corrected. Replaced value_json with value.