lolouk44
(lolouk44)
February 26, 2018, 4:00pm
1
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?
VDRainer
(🍻)
February 26, 2018, 4:10pm
2
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 -%}
?
gpbenton
(Graham)
February 26, 2018, 4:22pm
3
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
.
lolouk44
(lolouk44)
February 26, 2018, 4:46pm
4
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…
gpbenton
(Graham)
February 26, 2018, 6:01pm
5
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.
lolouk44
(lolouk44)
February 27, 2018, 12:39pm
6
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 )
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
lolouk44
(lolouk44)
February 27, 2018, 12:51pm
7
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
gpbenton
(Graham)
February 27, 2018, 1:28pm
8
If you see my working example, the value_template returns values matching the payload.
2 Likes
lolouk44
(lolouk44)
February 27, 2018, 1:42pm
9
Thanks that now makes sense to me
1 Like