#How to conditionally add brightness parameter to a single light action

1 messages · Page 1 of 1 (latest)

distant void
#

I have an automation that ends with a single light.turn_on action

  • action: light.turn_on
    metadata: {}
    data: {}
    target:
    entity_id: light.example_light
    I would like to conditionally add a brightness value to its data, behind a simple template
    data:
    brightness_pct: "{{ a_brightness_variable }}"
    but only if it's an integer (has been defined as an integer), otherwise an expected float error pops up.

How can I go around doing that? I can't duplicate the light turn on action since it's got quite a few branches and other (non optional) parameters set on the way.

covert briar
#

You can template the entire value of the data key as a dictionary:

action: light.turn_on
target:
  entity_id: light.example
data: |
  {{ {"brightness_pct": a_brightness_variable} if a_brightness_variable is integer else {} }}
distant void
#

ohh damn I did not realise that and it's absolutely perfect

left relic
#

the other option would be to duplicate the light.turn_on action - have one with the brightness, one without, and use a conditional statement (if) to pick which one to run. (edit: ah, you explained why that would be difficult)