MQTT Switch value template / state issue

Hi, I’m stuck with an MQTT switch.
I have a camera that I can control via MQTT.
This is what I have in my config:

- platform: mqtt
  name: "DaFang Kitchen RTSP"
  state_topic: "Kitchen/DaFang/status"
  value_template: "{% if value_json[RTSP-Server] == 'running' %} on {% else %} off {% endif %}"
  command_topic: "Kitchen/DaFang/set"
  payload_on: "h264_noseg_start"
  payload_off: "rtsp_stop"

The switching on/off bit works fine, but the status doesn’t and always returns off, no matter what.
Here is the topic message that the camera sends:

Topic					Message
Kitchen/DaFang/status	{"Uptime":" 15:15:31 up 2 days, 23:33, 0 users, load average: 3.20, 3.22, 3.23", "RTSP-Server":"running", "IR-Cut":"off", "Wifi":{"SSID":"VM230912", "Bitrate":"72.2 Mb/s", "SignalLevel":"94%", "Linkquality":"77%", "NoiseLevel":"0%"}, "LEDs":{"Blue":"off", "Yellow":"on", "Infrared":"off"}}

Interestingly, if I create n MQTT Sensor and use the same value_template, I get the correct state.
What am I missing?

Not sure, but i think your value_template returns ’ on ’ or ’ off '.
Can you try it without the spaces, or use hyphens in the jinja {%- if ... endif -%} ?

If that doesn’t work, I think the payloads of the state topic has match the payload of the command topics. So try replacing on and off with h264_noseg_start and rtsp_stop.

Thanks I did try by removing the space between } and on and between on and {
{% if value_json[RTSP-Server] == 'running' %}on{% else %}off{% endif %} I tried both suggestions and it still returns off all the time. Not sure what else to try… :frowning:

This looked puzzling, so I tried it out on my test box. The key was the error in the log

2018-02-26 17:54:56 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: 'RTSP' is undefined ...

Which meant the template wasn’t decoding correctly. The fix is to include it in escaped quotes

  - platform: mqtt
    name: "DaFang Kitchen RTSP"
    state_topic: "Kitchen/DaFang/status"
    value_template: "{% if value_json[\"RTSP-Server\"] == 'running' %}h264_noseg_start{% else %}rtsp_stop{% endif %}"
    command_topic: "Kitchen/DaFang/set"
    payload_on: h264_noseg_start
    payload_off: rtsp_stop

Which worked once for me.

Thanks @gpbenton but there’s something else at play here.
I played with as “simpler” entity to remove the quotes / dash issue.

I tried various ways as per below:

- platform: mqtt
  name: "test Blue1"
  state_topic: "LivingRoom/DaFang/status"
  value_template: "{{ value_json[\"LEDs\"][\"Blue\"] }}"
  command_topic: "LivingRoom/DaFang/set"
  payload_on: "blue_led_on"
  payload_off: "blue_led_off"
  optimistic: false

- platform: mqtt
  name: "test Blue2"
  state_topic: "LivingRoom/DaFang/status"
  value_template: '{%- if value_json.LEDs.Blue=="on" -%}on{% else %}off{% endif %}'
  command_topic: "LivingRoom/DaFang/set"
  payload_on: "blue_led_on"
  payload_off: "blue_led_off"
  optimistic: false

- platform: mqtt
  name: "test Blue3"
  state_topic: "LivingRoom/DaFang/status"
  value_template: '{{ value_json.LEDs.Blue }}'
  command_topic: "LivingRoom/DaFang/set"
  payload_on: "blue_led_on"
  payload_off: "blue_led_off"
  optimistic: false

- platform: mqtt
  name: "test Blue4"
  state_topic: "LivingRoom/DaFang/status"
  value_template: '{{ value_json.LEDs.Blue }}'
  command_topic: "LivingRoom/DaFang/set"
  payload_on: "on"
  payload_off: "off"
  optimistic: false

Only test blue4 returns on.
The “issue” is that the payload is the status AND the command, but the original camera firmware returns “on” as status and blue_led_on as a command. (well maybe the “issue” is me for not knowing / understanding how HA handles mqtt :wink: )
So I went back to the camera, change the status to show blue_led_on and here is the HA config that actually works, in case it helps someone else:

- platform: mqtt
  name: "test Blue3"
  state_topic: "LivingRoom/DaFang/status"
  value_template: '{{ value_json.LEDs.Blue }}'
  command_topic: "LivingRoom/DaFang/set"
  payload_on: "blue_led_on"
  payload_off: "blue_led_off"
  optimistic: false
1 Like

oh and for the RTSP entity, this is the code:

- platform: mqtt
  name: "Test RTSP"
  state_topic: "LivingRoom/DaFang/status"
  value_template: '{{ value_json["RTSP-Server"] }}'
  command_topic: "LivingRoom/DaFang/set"
  payload_on: "h264_noseg_start"
  payload_off: "rtsp_stop"
  optimistic: false

If you see my working example, the value_template returns values matching the payload.

2 Likes

Thanks that now makes sense to me :slight_smile:

1 Like