Json sensor help, please

I’ve got a Sonoff iFan02 ceiling fan controller that I’ve flashed with Tasmota. The Sonoff sends a status message to the same MQTT topic (stat/sonoff_MBR_fan/RESULT) for both the fan speed and the light condition (on or off) but containing different payloads.

If it is sending a fan speed status the resulting message payload is {“FanSpeed”:X} where X = the speed in numerical form from 0 to 3.

If it is sending the light status the message payload is {“POWER1”:“ON”} (or :“OFF”).

I want to create a sensor that only senses the payload when sending the status of the FanSpeed but ignores the status of the POWER1.

the following sensor will correctly read the changes in the fan speed as long as the power message doesn’t change;

- platform: mqtt
  name: MBR fan speed MQTT
  state_topic: "stat/sonoff_MBR_fan/RESULT"
  value_template: "{{ value_json.FanSpeed }}"

as I change the fan speed it will cycle thru 0-3 correctly. But as soon as I change the condition of the light the sensor shows as completely blank.

I’m pretty sure that’s because the ‘value_json.FanSpeed’ doesn’t exist in the message.

Here is the end use of the sensor which works as long as the sensor updates correctly:

- alias: 'MBR Fan Speed MQTT to RF'
  trigger:
    platform: event
    event_type: state_changed
    event_data:
      entity_id: sensor.mbr_fan_speed_mqtt
  action:
    service: homeassistant.turn_on
    data_template:
      entity_id: >
        {%- if states.sensor.mbr_fan_speed_mqtt.state | int == 0 -%}
          switch.master_bedroom_fan_0
        {%- elif states.sensor.mbr_fan_speed_mqtt.state | int == 1 -%}
          switch.master_bedroom_fan_1
        {%- elif states.sensor.mbr_fan_speed_mqtt.state | int == 2 -%}
          switch.master_bedroom_fan_2
        {%- elif states.sensor.mbr_fan_speed_mqtt.state | int == 3 -%}
          switch.master_bedroom_fan_3
        {%- endif -%}

How can I set up the sensor value_template to ignore any message that doesn’t contain ‘FanSpeed’?

so if I get this right, the message contains both fan speed and light status, but if you just change the light status, the fan speed is not automatically provided in the message? If so the fan speed sensor will indeed return blank.

I think something like this could help:

  - platform: mqtt
    state_topic: "home/rtl_433"
    name: "Aussen Temp Nexus"
    unit_of_measurement: "°C"
    value_template: >
      {% if value_json is defined and value_json.id == 137 %}
        {{ value_json.temperature_C | round(1) }}
      {% else %}
        {{ states('sensor.aussen_temp_nexus') }}
      {% endif %}

@lolouk44

Not quite…

If i change the fan speed the topic (stat/sonoff_MBR_fan/RESULT) will return only the payload for the fan speed status {“FanSpeed”:X}

If I turn the light off or on the same topic will return only the payload for the light status {“POWER1”:“ON”}

So would the ‘value_json.id’ be ‘FanSpeed’?

No, something like

{% if value_json.FanSpeed is defined %}

@VDRainer

Ah Ok.

then should I try:

value_template: >
  {% if value_json.FanSpeed is defined %}
    {{ value_json.FanSpeed }}
  {% endif %}

EDIT:

Tried the above and had the same result - changing the light status wipes out the sensor value until the fan speed is changed again.

Then try it with

value_template: >
  {% if value_json.FanSpeed is defined %}
    {{ value_json.FanSpeed }}
  {% else %}
    {{ states('sensor.mbr_fan_speed_mqtt') }}
  {% endif %}

So the sensor gets his own value if there’s no FanSpeed defined.

1 Like

That worked like a charm!

Thank you very much! :smile:

Hi @VDRainer,
For my own learning/understanding, and possibly a silly question, but would that not potentially run into an infinite loop when value_json.FanSpeed is not defined? Or is HA clever enough to handle this situation?

Hi @lolouk44, good point, but i think the value_template gets only evaluated on a payload at the mqtt topic.

Not sure about this, but i have around 10 sensors like the one in my first post (all the same topic, with different id’s) and don’t see any impact.

1 Like