rdlaner
(Dustin)
January 1, 2020, 9:12pm
1
Hey folks, I’m trying to set up an automation that is triggered by an MQTT topic and then sets the value of an input number slider based on data from the MQTT topic. The trigger seems to be working fine, but the action that is supposed to update the input number value is not doing anything. I tried using the template editor, but it doesn’t seem to work for automations.
I suspect that I’m not parsing the trigger payload correctly in the data_template. Any insight on what I may be doing wrong?
Here’s the yaml:
- alias: Kitchen_RGBW_Sub
id: Kitchen_RGBW_Sub
trigger:
platform: mqtt
topic: "stat/Lights/Kitchen_Cab/STATE"
action:
- service: input_number.set_value
data_template:
entity_id: input_number.red_slider
value: "{{ trigger.payload.value_json.Color.split(',')[0] | int}}"
- service: input_number.set_value
data_template:
entity_id: input_number.green_slider
value: "{{ trigger.payload.value_json.Color.split(',')[1] | int }}"
- service: input_number.set_value
data_template:
entity_id: input_number.blue_slider
value: "{{ trigger.payload.value_json.Color.split(',')[2] | int }}"
- service: input_number.set_value
data_template:
entity_id: input_number.white_slider
value: "{{ trigger.payload.value_json.Channel[3] | int }}"
francisp
(Francis)
January 2, 2020, 7:16am
2
You could help if you posted the actual payload.
rdlaner
(Dustin)
January 2, 2020, 7:50am
3
Yes, of course. The data is published in json, here is an example:
{
"Channel": [
0,
0,
0,
255
],
"Color": "0,0,0",
"POWER": "OFF",
"Dimmer": 1
}
francisp
(Francis)
January 2, 2020, 8:11am
4
In the template editor :
Imitate available variables:
{% set my_test_json = {
"Channel": [
0,
0,
0,
255
],
"Color": "0,0,0",
"POWER": "OFF",
"Dimmer": 1
} %}
value: "{{ my_test_json.Color.split(',')[0] | int}}"
returns “0”, did not expect that.
francisp
(Francis)
January 2, 2020, 8:15am
5
Try this :
Imitate available variables:
{% set my_test_json = {
"Channel": [
0,
0,
0,
255
],
"Color": "0,0,0",
"POWER": "OFF",
"Dimmer": 1
} %}
value: {{ my_test_json.Color.split(',')[0] | int }}
value is now 0
rdlaner
(Dustin)
January 2, 2020, 8:50pm
6
I need the quotations around {{ my_test_json.Color.split(’,’)[0] | int}} in order for the automations.yaml file to be valid. But it does appear that my parsing is correct from the .value_json onward.
So does that indicate that either “trigger.payload” is not the correct value to use or maybe the " - service: input_number.set_value" is not correctly used?
francisp
(Francis)
January 3, 2020, 6:19am
7
To be honest, I’m lost here too. Both seem correct.
francisp
(Francis)
January 3, 2020, 1:56pm
8
Here they use the trigger.payload.value_json successfully
That seems to be the magic with HA, just keep restarting. This is my final automation format:
- id: '1577995477209'
alias: MQTT Dimmer
description: ''
trigger:
- platform: mqtt
topic: room/dimmer/set
condition: []
action:
- data_template:
brightness: '{{ trigger.payload_json.brightness|int }}'
entity_id: light.living_room
service_template: >
light.turn_{{ trigger.payload_json.state }}
with this mqtt:
{
"topic":"room/dimmer/set",
"payload": {"state…
I used trigger.payload_json, not payload.value_json
francisp
(Francis)
January 3, 2020, 2:07pm
10
Try
"{{ trigger.payload_json.Color.split(',')[0] | int}}"
rdlaner
(Dustin)
January 4, 2020, 7:03am
11
Thanks for the input everybody! It looks like changing to using payload_json
did the trick. Here’s what I ended up using:
- alias: Kitchen_RGBW_Sub
id: Kitchen_RGBW_Sub
trigger:
platform: mqtt
topic: "stat/Lights/Kitchen_Cab/STATE"
action:
- service: input_number.set_value
data_template:
entity_id: input_number.red_slider
value: "{{ trigger.payload_json.Color.split(',')[0] | int }}"
- service: input_number.set_value
data_template:
entity_id: input_number.green_slider
value: "{{ trigger.payload_json.Color.split(',')[1] | int }}"
- service: input_number.set_value
data_template:
entity_id: input_number.blue_slider
value: "{{ trigger.payload_json.Color.split(',')[2] | int }}"
- service: input_number.set_value
data_template:
entity_id: input_number.white_slider
value: "{{ trigger.payload_json.Channel[3] | int }}"