My MQTT farm sensor entity flips between working and entity unavailable

Hi All!

I have my sensor working over MQTT almost perfectly. My problem is that the Dashboard card flips between showing a temperature and “entity unavailable” on every reading:

“RasberryPi1” takes the reading from a ruuvi gateway and the following node-red config sends the reading to the topic->a “trigger:state” node reads the temp and sends it via a “mqtt_out” node to my topic:

“RasberryPi2” reads from my MQTT topic and creates a sensor

I can see that the way the ruuvi gateway works, there is some time in between readings where the state goes to “NaN” and that must be why i get this flip flopping. Anyone know how to set my sensor to ignore the moments where there is no reading available? I dont want my dashboard to flicker like this : )

NAN means not a number. There should be " " round the temperature and add a template line to make sure it’s a number

device_class: "temperature"
value_template: "{{ (value |float(0) ) | int }}"

If that doesn’t work then using mqttexplorer show what the payload of the topic farm is and what it changes to when it goes unavailable. Is that trigger state reda_ruuvi_temp a number or a string?

Thanks for the advice. It’s still flashing…digging in a bit more now

hmm…turns out it never goes “unavailable” when i watch in real-time on MQTT explorer:

I’ts gotta be my MQTT config under configuration.yaml

It looks like its reading one number as a number then the next as a string i.e. Not A Number. I would have thought my value template should have made it turn the string into a number. Just checking you reloaded mqtt yaml after adjusting the yaml.

  - trigger:
      - platform: state
        entity_id: ruuvi_temp
          - unavailable
          - NaN
      - name: farm fridge Temperature
        state: '{{ trigger.to_state.state }}'
        device_class: temperature
        unit_of_measurement: '°F'

An alternative I saw here was to create a new sensor based on a template of the old sensor seen here 123’s solution. Now you end up with 2 sensors.