Template sensors are unavailable or unknown state on HA restart problem

“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.

I’m curious to know why you chose to define this sensor as a:
Trigger-based Template Sensor with an MQTT Trigger

instead of an:
MQTT Sensor

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).

1 Like

I have other mqtt sensors:

template:
- trigger:
  - platform: mqtt
    topic: home-assistant/sensor03/messaggio
  sensor:
  - name: mqttfrequency
    state: "{{ trigger.payload.split(',')[8] }}"
    unit_of_measurement: "Hz" 
  - name: mqttcurrentpdc
    state: "{{ trigger.payload.split(',')[7] }}"
    unit_of_measurement: "A" 
    device_class: current
  - name: mqttpowerpdc
    state: "{{ trigger.payload.split(',')[6] }}"
    unit_of_measurement: "W" 
    device_class: power
  - name: mqttenergypdc
    state: "{{ trigger.payload.split(',')[5] }}"
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: "kWh" 
  - name: mqttvoltagepdc
    state: "{{ trigger.payload.split(',')[4] }}"
    unit_of_measurement: "V" 
    device_class: voltage
  - name: mqttcurrent
    state: "{{ trigger.payload.split(',')[3] }}"
    unit_of_measurement: "A" 
    device_class: current
  - name: mqttpower
    state: "{{ trigger.payload.split(',')[2] }}"
    unit_of_measurement: "W" 
    device_class: power
  - name: mqttenergy
    state: "{{ ( trigger.payload.split(',')[1] | float / 1000 ) | round (1) }}"
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: "kWh" 
  - name: mqttvoltage
    state: "{{ trigger.payload.split(',')[0] }}"
    unit_of_measurement: "V" 
    device_class: voltage   

I received this solution from an user of the community, he helped me to “divide” the array…

Seems correct. At reboot, “sensor.mqttenergy” will only be populated the next time the array is sent, unless it is sent as a “retained” message.

So the max time between reboot and the sensor being populated should be 30s. Is it not what you are seeing?

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, … :wink:

They’re not MQTT Sensors.

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.

it’s retained

Not sure a MQTT-triggered template will be triggered by a retained message at startup.
It looks like not…

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.

Perfect. Convert to using MQTT Sensor and the problem will be eliminated.

sensor:
  - platform: mqtt
    name: mqttfrequency
    state_topic: home-assistant/sensor03/messaggio
    unit_of_measurement: "Hz"
    value_template: "{{ value.split(',')[8] }}"
  - platform: mqtt
    name: mqttcurrentpdc
    state_topic: home-assistant/sensor03/messaggio
    unit_of_measurement: "A"
    value_template: "{{ value.split(',')[7] }}"
    device_class: current
  - platform: mqtt
    name: mqttpowerpdc
    state_topic: home-assistant/sensor03/messaggio
    unit_of_measurement: "W"
    value_template: "{{ value.split(',')[6] }}"
    device_class: power

... etc ...

NOTE

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.

2 Likes

this is the payload of “messaggio”:
228.3,4932783.0,535.0,2.8,227.9,427.8,95.2,0.6,50.0

in fact now the sensors have “unknown” value…

In that case, the template should refer to value and not value_json because the payload is not in JSON format.

{{ value.split(',')[0] }}

I have updated my example (posted above) with the revised template.

yes, now I see the readings!
now I’ll perform some reboot to see if there are bad readings…

the problem remains…


when I reboot the raspberry or when I restart HA, the energy consumption has a peak…

Just now you said this:

but originally you said this:

Is that the same problem as the original one or different?

Do the MQTT Sensors still report unavailable or unknown on startup?

the problem is always the same… maybe I couldn’t explain myself…
after a restart the energy has a peak. the peak has the value of the sensor.mqttenergy, this sensor transmit a rising value (at the moment it is 6020577.0 Wh)… the graph should shows the difference between two readings, so normally few kwh (according the energy consumption of my home)…
I don’t know really if after a reboot the sensor has a unavailable or unknown status, I done a supposition because the graph doesn’t show the difference between two values

That’s quite different from what you originally said twenty-two posts earlier:

I guess your supposition was wrong (and so is the topic’s title).

Anyway, you are now using MQTT Sensors which means they will have known values on startup.

Maybe I made confusion by reading this:

there is my same problem, and he resolved by using “availability”, in my first post I speak about this…