Template sensors are unavailable or unknown state on HA restart problem

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…

availability will solve that problem if the sensor reports zero on startup. I’ve said this a few times now. If your sensor is unavailable at startup, it will not have this jump. Using availability templates will make your sensor unavailable at startup. So the issue is somewhere else if availability is not working.

1 Like

Show what you currently have.
Obviously, the sensor is not divided by 1000 anymore, and you still have zeroes (or other low values), which explain the spikes.

This is my conf:

homeassistant:
  customize:
    sensor.mqttenergy:  
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "kWh"
      friendly_name: "Energia Assorbita Casa"

sensor:
    - platform: mqtt
      name: mqttenergy
      state_topic: home-assistant/sensor03/messaggio
      value_template: "{{ value.split(',')[1] }}"
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "kWh" 	  

this is the actual value:

and the actual energy consumption:

Why are you duplicating these options:

  • device_class
  • state_class
  • unit_of_measurement

in customize when they already exist in the MQTT Sensor’s configuration?

Anyway, it looks like it could benefit from availability_template.

1 Like

You don’t say…

1 Like

So, do you suggest to use availability like this:

sensor:
    - platform: mqtt
      name: mqttenergy
      state_topic: home-assistant/sensor03/messaggio
      value_template: "{{ value.split(',')[1] }}"
      availability: "{{ ( value.split(',')[1] | int ) > 0 }}"
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "kWh"

You should be using the availability_template option, not the availability option which serves another purpose (refer to the documentation).

Replace that line with this:

      availability_topic: home-assistant/sensor03/messaggio
      availability_template: "{{ value.split(',')[1] | int(-1) > 0 }}"

availability_template: "{{ value.split(',')[1] | is_number }}"

EDIT: Or what 123 posted

with:

    - platform: mqtt
      name: mqttenergy
      state_topic: home-assistant/sensor03/messaggio
      value_template: "{{ value.split(',')[1] }}"
      availability_topic: home-assistant/sensor03/messaggio
      availability_template: "{{ value.split(',')[1] | int(-1) > 0 }}"
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "kWh" 

or

    - platform: mqtt
      name: mqttenergy
      state_topic: home-assistant/sensor03/messaggio
      value_template: "{{ value.split(',')[1] }}"
      availability_topic: home-assistant/sensor03/messaggio
      availability_template: "{{ value.split(',')[1] | is_number }}"
      device_class: energy
      state_class: total_increasing
      unit_of_measurement: "kWh" 

the sensor is always unavailable…
image

are there errors in your logs?

no, there aren’t

and your topic is changing?

do you mean the value of the array? yes, it is always changing, each 30s…

Then either template should be working. None of us have your setup though, so only you can debug this.