Automatation Match mqtt dimmer to light

Hey guys,

i am going a bit crazy and tried to adapt some of the automations …but with no luck.

I have a tradfri dimmer which i want to match with one of my lights… I created this automation for this:

- id: '1566242994281'
  alias: Ikea
  trigger:
  - platform: mqtt
    topic: zigbee2mqtt/0x000b57fffe728_brightness
  condition: []
  action:
  service: light.turn_on
  entity_id: light.wand
  data_template:
    brightness: state_attr(0x000b57fffe728_brightness','brightness')

could you give me a hint why I get a error in the log like :

Error while executing automation automation.ikea. Invalid data for call_service at pos 1: expected int for dictionary value @ data['brightness']

every info is much appreciated!

Correct way to structure a template:

  data_template:
    brightness: "{{ state_attr('0x000b57fffe728_brightness','brightness') | int }}"

But that won’t work. 0x000b57fffe728_brightnes is not an entity id. You need to extract the brightness from trigger.payload

If the payload is an integer this should work:

  data_template:
    brightness: "{{ trigger.payload }}"

But without knowing what the message sent to the topic looks like this is just a guess.

1 Like

Hey thanks for the fast reply!

I adjusted the template but still there is no effect

- id: '1566249261533'
  alias: Ikea off
  trigger:
    platform: state
    entity_id: sensor.0x000b57fffe72fbd8_brightness
  condition:
    condition: template
    value_template: "{{  trigger.payload|int == 0 }}}"
  action:
    service: light.turn_off
    entity_id: light.flur_unten
- id: '1566249317287'
  alias: Ikea on
  trigger:
    platform: state
    entity_id: sensor.0x000b57fffe72fbd8_brightness
  condition:
    condition: template
    value_template: "{{ trigger.payload|int != 0}}"
  action:
    service: light.turn_on
    entity_id: light.wand
    data_template:
      brightness: "{{ trigger.payload|int}}"

And in the Logfile this apears any time I trigger the dimmer:

2019-08-20 22:03:02 ERROR (MainThread) [homeassistant.helpers.condition] Error during template condition: UndefinedError: 'dict object' has no attribute 'payload'

``

That’s because you changed this:

  trigger:
    platform: mqtt
    topic: zigbee2mqtt/0x000b57fffe728_brightness

to

  trigger:
    platform: state
    entity_id: sensor.0x000b57fffe72fbd8_brightness

"{{ trigger.payload }}"only works with the mqtt trigger.

If you are going to use the state trigger use this template:

  data_template:
    brightness: >
      {{ state_attr('sensor["0x000b57fffe728_brightness"]','brightness') | int }}

Square brackets are required because your object id begins with a number. https://www.home-assistant.io/docs/configuration/templating/#entity_id-that-begins-with-a-number

Okay sounds like I owe big pint …and… Iam gettin really confused with the automation

I guess I will stick to the mqtt plattform because many other devices in my setup are running with mqtt.

therefore i used this code… because lucky me … i realized …i was wrong with the mqtt topic … but i corrected it and adjusted it but at least no error so far… and no reaction

- id: '1566249261533'
  alias: Ikea off
  trigger:
  - payload: brightness
    platform: mqtt
    topic: zigbee2mqtt/0x000b57fffe72fbd8
  condition:
  - condition: template
    value_template: '{{trigger.payload|int ==0}}}'
  action:
  - data:
      entity_id: light.wand
    entity_id: light.wand
    service: light.turn_off
- id: '1566249317287'
  alias: Ikea on
  trigger:
    platform: mqtt
    topic: zigbee2mqtt/0x000b57fffe72fbd8
  condition:
  - condition: template
    value_template: '{{trigger.payload|int != 0}}'
  action:
  - data:
      entity_id: light.wand
    data_template:
      brightness: '{{ trigger.payload|int}}'
    entity_id: light.wand
    service: light.turn_on

But i guess i did there something wrong because in home assistant it self the output is diffrent.

here the example for the ikea on example:

further I double checked with mqtt that i got the right topic in mqtt:

I think I still can’t match the mqtt info of the brightness to the brightness of the lamp … I feelin really dump at this task ^.^

Your patience is really appreciated!