Template sensors are unavailable or unknown state on HA restart problem

Not sure what to tell you. The template is correct in regards to what has been shown from you. Did you try 123s?

If think it should be

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

Per the doc of ā€œavailability_templateā€

1 Like

This. :man_facepalming:

I missed that important detail; the default values for reporting availability/unavailability arenā€™t true/false but online/offline.

@enry86cami

Use either the corrected template posted by koying:

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

or this one I suggested earlier:

      availability_template: "{{ 'online' if value.split(',')[1] | int(-1) > 0 or 'offline' }}"

The only difference between the two is that the second one will also reject negative values. So if a negative value is received it will make the sensor unavailable and the negative value will not be reported. If you want it to report any negative values received, use koyingā€™s version.

thanks to all for the support.
with this code:

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

the sensor works, but the peaks in energy module remainsā€¦
a strange thing:
when I reboot the sensor shows for few seconds this value:
image

and after the correct value:
image

I see this value also there:

ā€¦maybe i found the problem:
there was only one value retained. so at each reboot, HA sees the ā€œretained wrong valueā€ 4932783.
so the peak is the result of 6060304-4932783ā€¦
I supposeā€¦

I donā€™t know why you refer to it as a ā€œwrongā€ value because, based on the last screenshot in your post, itā€™s clear that the energy module is actually publishing it. In other words, itā€™s not due to any fault of Home Assistant misrepresenting the information; itā€™s what is actually being received.

You should be investigating the energy module to determine why it behaves the way it does.

TBH the value in general looks off to me. Houses average 893 kWh per month and enry86cami is sitting at 5 million kwh. In general, I think the sensor is being miss understood and used.

Itā€™s an aluminum smelter. :upside_down_face:

Cheese and rice, how many tons is this thing makingā€¦ 300 tons?

I suppose you could filter out any value greater than a given threshold. For example, this will report the sensor is available only if the value is between zero and ten thousand (adjust it to suit your needs).

      availability_template: "{{ 'online' if 0 < value.split(',')[1] | int(-1) < 10000 or 'offline' }}"

with the software MQTT.fx, after connected to the MQTT broker I always had this first value retained:

but is an old value.

with the software MQTT EXPLORER I removed this value and now I donā€™t have a value retained.
with MQTT EXPLORER I found other old sensor with retained values. these are old sensor actually disconnected to the LAN. So I performed a clean of the ā€œOLD MQTT PACKAGEā€ in my lan.
In fact now, by MQTT.fx I donā€™t see anymore a retained value:

But I donā€™t know how could be happened that some value had retainedā€¦

So I suppose that my problem was this old value ā€œin pendingā€ into the lanā€¦

In your latest screenshot, itā€™s reporting over 6 million kWh. Is that correct?

If it is correct, what is this sensor monitoring?

Yes, it is correct but the unit of measurement is Wh, so it is 6.000kWh. About one year of my home electric consumption.

The unit_of_measurement you specified in the sensorā€™s configuration is kWh. I suggest you modify the sensorā€™s value_template so it converts the received value, in Wh, to a reported value in kWh.

      value_template: "{{ (value.split(',')[1] | int(0)) / 1000 }}"

youā€™re right, my conf works only because the HA energy module works with differences

Hi

Having the same problem but with awatewr comsuption. I have a sensor taht starts with 0 when I restart the HA and seconds later recover last values.

  • platform: template
    sensors:
    diario_agua_disponible:
    friendly_name: ā€œdiario agua availableā€
    entity_id: sensor.diario_agua_con_acumulado_estimado_duplicated
    value_template: ā€œ{{ (states(ā€˜sensor.diario_agua_con_acumulado_estimado_duplicatedā€™) | float ) | round(2) }}ā€
    availability_template: ā€œ{{ not is_state(ā€˜sensor.diario_agua_con_acumulado_estimado_duplicatedā€™, ā€˜unknownā€™,ā€˜0.0ā€™, ā€˜0,0ā€™ ) }}ā€

I can observe taht sensor starts as a Unknown for a seconds , after that change to 0.0 and after that to the correcta value.

Impossible to keep it unavailable till have the correct value.

Any recomendation?