How to (re)use sensor value in automation

Hey there

Hoping someone can help me - I’m trying to set the brightness of two lights based on the actual outside brightness. I so far have a sensor setup that is reading brightness via MQTT and is available as sensor.outside_brightness with int value between 1 and 255.

Sensor config:

   - platform: mqtt
     state_topic: "kleikamp_brightness"
     name: "Outside Brightness"
     unit_of_measurement: "bright"
     value_template: '{{ value_json.Brightness }}'

Automation config:

- id: matchbrightness
  alias: Match Outside Brightness
  trigger:
    platform: mqtt
    topic: kleikamp_brightness
  action:
    service: light.turn_on
    entity_id:
      - light.floalt_panel_ws_60x60
      - light.floalt_panel_ws_60x60_2
    data:
      brightness: '{{ states.sensor.outside_brightness }}'

I get the error:

Jun 18 15:41:55 localhost hass[6677]: ERROR:homeassistant.core:Invalid service data for light.turn_on: expected int for dictionary value @ data['brightness']. Got '{{ states.sensor.outside_brightness }}'

Obviously it’s not getting passed the actual value - I’ve tried sensor.outside_brightness also with no joy… any ideas?

Hi! What about this?

{{ states.sensor.outside_brightness.state }}

Good luck!

Hmm sadly that didn’t work.

Now I’m thinking I need to use “automation templating” - trying this:

brightness: '{{ trigger.payload_json.Brightness }}'

But that doesnt work either :confused:

Thanks for the pointers… OK so I solved it - it was automation templating, I needed to use “data_template” instead of “data”. Here’s the working automation:

- id: matchbrightness
  alias: Match Outside Brightness
  trigger:
    platform: mqtt
    topic: kleikamp_brightness
  action:
    service: light.turn_on
    entity_id:
      - light.floalt_panel_ws_60x60
      - light.floalt_panel_ws_60x60_2
    data_template:
      brightness: '{{ trigger.payload_json.Brightness }}'

Basically works such that the ikea panels behave like skylights :smile:

1 Like

Glad you fixed it and thanks for sharing the solution. You may consider to mark the post as solved and point your answer in #4.