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

ok and what does the graph look like?

Sorry, this is the correct one. Your automation was for airco energy. I took the wrong one. But you can see that the issue is here too.

I guess they don’t have the order of operations correct with availability and value_template for the MQTT platform.

remove the availability stuff then and use this as a value_template

value_template: >
  {% set avail = value_json.channelPowers | map(attribute='power') | list | sum != 0 %}
  {% set result = (value_json.channelPowers[0].power + value_json.channelPowers[1].power + value_json.channelPowers[2].power) | round(0) %}
  {{ result if avail else none }}

This seems to work. Now when I post the faulty json the entity state changes to “None” and I only see a gap.

I adjusted all 120 entities with the new value template and tested multiple times. It seems to work well now.

So the problem is a bug in HA MQTT integration?

Yep  

I’m not sure what ‘dasf asdfka efaewf’ means but I was wondering if there are optimisations possible regarding calculating the sum of all power attributes. Isn’t it now calculated 120 times every 15 seconds? I wonder if HA has some way to optimise it so that doesn’t have to calculate it again that many times

There’s a stupid 10 character limit for replies. And yes, I created an issue for this as you shouldn’t be getting zero with those. The resolution order does not follow Template sensors.

1 Like

Yes it is, but it’s a simple calc, nothing that will take long or be taxing, if you want to optimize it, you can toss the values into attributes and then use template sensors to extract it. Not worth it IMO.

Not sure if MQTT Sensor supports this.state but see if this eliminates the gap.

{{ result if avail else this.state }}

Otherwise replace this.state with the states() function referencing the sensor’s own entity_id.

The idea is to report the sensor’s existing value (i.e. previous value received from a valid payload).

actually, you could just use template in general. But keep in mind that you won’t be able to set the object_id

template:
- trigger:
  - platform: mqtt
    topic: servicelocation/1a9cc55c-89d1-4823-896d-99a12ca0b4f2/realtime
    variables:
      value_json: "{{ trigger.payload_json }}"
      available: "{{ value_json.channelPowers | map(attribute='power') | list | sum != 0 }}"
  sensor:
  - name: Airco Power
    unique_id: smappee-watt-34817A+34817B+34817C
    state: "{{ (value_json.channelPowers[0].power|float + value_json.channelPowers[1].power + value_json.channelPowers[2].power) | round(0) }}"
    availabilty: "{{ available }}"

  - name: Airco Power 2
    unique_id: smappee-watt-34817A+34817B+34817C
    state: "{{ (value_json.channelPowers[0].power|float + value_json.channelPowers[1].power + value_json.channelPowers[2].power) | round(0) }}"
    availabilty: "{{ available }}"
1 Like