floris-b
(Floris B)
June 21, 2018, 2:42pm
1
Hi All,
Does anybody know how to get " states.[trigger.payload_json.entity_id].attributes.brightness " working?
entity_id is send as JSON in the payload so you can dim multiple lights with one automation
- alias: Dim light
trigger:
- platform: mqtt
topic: /dimlight
action:
- service: light.turn_on
data_template:
entity_id: '{{ trigger.payload_json.entity_id }}'
brightness_pct: '{{ states.[trigger.payload_json.entity_id].attributes.brightness + 5 }}'
petro
(Petro)
June 21, 2018, 3:46pm
2
Is entity_id inside your payload? If not…
- alias: Dim light
trigger:
- platform: mqtt
topic: /dimlight
action:
- service: light.turn_on
data_template:
entity_id: '{{ trigger.entity_id }}'
brightness_pct: '{{ states.[trigger.entity_id].attributes.brightness_pct + 5 }}'
floris-b
(Floris B)
June 21, 2018, 4:01pm
3
That is also not going to work, I just used the [] to make clear what I needed to replace but that does not work and gives a invalid template error
petro
(Petro)
June 21, 2018, 4:02pm
4
Ah, nevermind i see the issue:
- alias: Dim light
trigger:
- platform: mqtt
topic: /dimlight
action:
- service: light.turn_on
data_template:
entity_id: '{{ trigger.entity_id }}'
brightness_pct: >
{% set domain, device = trigger.entity_id.split('.') %}
{{ states[domain][device].attributes.brightness_pct + 5 }}
or
- alias: Dim light
trigger:
- platform: mqtt
topic: /dimlight
action:
- service: light.turn_on
data_template:
entity_id: '{{ trigger.entity_id }}'
brightness_pct: "{{ state_attr(trigger.entity_id,'brightness_pct') | int + 5 }}"
floris-b
(Floris B)
June 21, 2018, 4:51pm
5
Thanks a lot, I got it working now with:
- alias: Dim light
trigger:
- platform: mqtt
topic: /dim
action:
- service: light.turn_on
data_template:
entity_id: '{{ trigger.payload_json.entity_id }}'
brightness: >
{% set domain, device = trigger.payload_json.entity_id.split('.') %}
{% set step = trigger.payload_json.step %}
{% set value = states[domain][device].attributes.brightness + step %}
{{ value }}