Template sensors are unavailable or unknown state on HA restart problem

Hi all,
I have this template:

template:
- trigger:
  - platform: mqtt
    topic: home-assistant/sensor03/messaggio
  sensor:
  - name: mqttenergy
    state: "{{ ( trigger.payload.split(',')[1] | float / 1000 ) | round (1) }}"
    availability: "{{ ( trigger.payload.split(',')[1] | int ) > 0 }}"
    device_class: energy
    state_class: total_increasing
    unit_of_measurement: "kWh" 

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.

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?