MQTT sensors randomly read value 0 while "0" is never posted to MQTT server?

I’m trying to figure out how to do this.
I already had this for the energy sensors but they still drop to 0 randomly:

    availability_template: "{% if ( ( (value_json.channelPowers[3].importEnergy|float) + (value_json.channelPowers[4].importEnergy|float) + (value_json.channelPowers[5].importEnergy|float) ) ) == 0 %}offline{% else %}online{% endif %}"
    availability_topic: "servicelocation/1a9cc55c-89d1-4823-896d-99a12ca0b4f2/realtime"
    availability_mode: all

I guess the availability_template is not what I need?

With an availability template & anchors

mqtt:
 sensor:
  - name: "Device1"
    unique_id: "smappee-watt-device1"
    object_id: "smappee-watt-device1"
    value_template: "{{ (value_json.channelPowers[35].power|float) | round(0) }}"
    <<: &deviceitems
      state_topic: "servicelocation/1a9cc55c-89d1-4823-896d-xxxxxxxf2/realtime"
      unit_of_measurement: "W"
      payload_available: True
      payload_not_available: False
      availability_topic: servicelocation/1a9cc55c-89d1-4823-896d-xxxxxxxf2/realtime
      availability_template: >
        {{ value_json.channelPowers | map(attribute='power') | list | sum != 0 }}

each additional sensor would just be

  - name: "Device2"
    unique_id: "smappee-watt-device2"
    object_id: "smappee-watt-device2"
    value_template: "{{ (value_json.channelPowers[36].power|float) | round(0) }}"
    <<: *deviceitems

Basically, you’re adding an availability template that checks if the sums of all the powers in the list are 0. If that’s the case, it’s not available for that moment. It will become available as soon as all the readings have a sum greater than zero. That moment in time will have ‘missing’ data, but it won’t affect the totals like 0 would.

You don’t have to use anchors, you can just list them out too…

mqtt:
 sensor:
  - name: "Device1"
    unique_id: "smappee-watt-device1"
    object_id: "smappee-watt-device1"
    value_template: "{{ (value_json.channelPowers[35].power|float) | round(0) }}"
    state_topic: "servicelocation/1a9cc55c-89d1-4823-896d-xxxxxxxf2/realtime"
    unit_of_measurement: "W"
    payload_available: True
    payload_not_available: False
    availability_topic: servicelocation/1a9cc55c-89d1-4823-896d-xxxxxxxf2/realtime
    availability_template: >
      {{ value_json.channelPowers | map(attribute='power') | list | sum != 0 }}

  - name: "Device2"
    unique_id: "smappee-watt-device2"
    value_template: "{{ (value_json.channelPowers[36].power|float) | round(0) }}"
    state_topic: "servicelocation/1a9cc55c-89d1-4823-896d-xxxxxxxf2/realtime"
    unit_of_measurement: "W"
    payload_available: True
    payload_not_available: False
    object_id: "smappee-watt-device2"
    availability_topic: servicelocation/1a9cc55c-89d1-4823-896d-xxxxxxxf2/realtime
    availability_template: >
      {{ value_json.channelPowers | map(attribute='power') | list | sum != 0 }}

But the advantage of anchors is to reduce redundancy

OK, so the testing procedure was at fault and misleading you to believe the Smappee device wasn’t reporting 0 at certain times (but it actually was reporting 0).

You could change the template to replace a received 0 with the sensor’s existing value or use availability. However that effectively eliminates the possibility of ever reporting 0 even if it’s the true value. Is that acceptable for your application?

Thanks you very much. I’m going to recreate my configuration file with anchors and use your suggestion to use the sum of all power to make the entities available/unavailable.

I’ll report back once done :slight_smile:

1 Like

I made some errors and corrected them. Make sure you refresh the page here to see the fixes.

  - name: "Airco (Power)"
    unit_of_measurement: "W"
    object_id: "smappee-watt-airco"
    unique_id: "smappee-watt-34817A+34817B+34817C"
    <<: &common_power_items
      state_topic: "servicelocation/1a9cc55c-89d1-4823-896d-99a12ca0b4f2/realtime"
      availability_topic: "servicelocation/1a9cc55c-89d1-4823-896d-99a12ca0b4f2/realtime"
      availability_template: "{{ value_json.channelPowers | map(attribute='power') | list | sum != 0 }}"
    value_template: "{{ ((value_json.channelPowers[0].power|float) + (value_json.channelPowers[1].power|float) + (value_json.channelPowers[2].power|float)) | round(0) }}"

It’s now “unavailable” all the time. Did I miss something?

Nope, are all the values greater than 0? Are there errors in your logs?

The sum of all channelPower entries is certainly bigger than 0. No errors in the logs.

probably need to add this to the anchor

payload_available: True
payload_not_available: False

Yes, that fixed it. Thanks!! :slight_smile:

1 Like

Tested and confirmed working. If I now repost the faulty json all entities become unavailable untill smappe posts a new, correct, json topic. No more 0 in my charts :slight_smile:

Thanks to all who helped and especially @petro .

1 Like

Oh… wtf :slight_smile: I was too quick to say it’s solved.
The sensors do become unavailable after reposting the faulty json but the charts still DO drop to 0 too :frowning:

the charts may drop to zero, but it shouldn’t add the value when revived. It should purely be just a display for the unavailable value.

Keep in mind these changes aren’t retroactive, they will only apply this point forward

I do understand that it’s not retroactive but I resent the faulty json to MQTT a couple of times and every time I can see the HA chart going to 0 like before.

If I hover over that point it still shows “0 W” like it did before.

Can you show me which graph you’re referring to? Have you cleared your cache? Also, does the MQTT sensor show unavailable?

You shouldn’t have data to hover over.

This is what your history will look like:

This is an example of such a chart. At the right you see it going to 0 multiple times. This happend after the changes, after restarting HA, and after I reposted the faulty json manually.

does the entity go unavailable? Force it unavailable, it should have the word unavailable.

I see 0W when hovering over the last point, when I manually sent the faulty json.

I can see the entity going “unavailable” every time I manually repost that json. It comes back as soon as the smappee devices posts a new, correct, json.

No you still aren’t understanding.

I only care about what it says here:
image

There it changes to “unavailable” when I repost the faulty json.

But I was expecting a gap in the chart instead of the chart going to 0 ?