Please help with automation with if/else in service-data

I’d love to get someone’s eyes on this, basically I had four automations (that worked) for setting the brightness of the led’s in our closets based on the period of the day (template sensor).

For sake of mainenance and ease of use I wanted to boil it down to one automation with if/else in the action, can’t get it to work as I expected, the config validates fine but nothing happens when triggered. Any idea what I’m doing wrong? THanks in advance!

- id: closetsBrightness
  alias: 'Closets brightness'
  initial_state: 'on'
  trigger: 
    platform: state
    entity_id: sensor.period_of_day
  action:
    - service: light.turn_on
      entity_id: light.closets
      data:
        brightness: >- 
          {% if is_state("sensor.period_of_day", "dawn") %}
            126
          {% elif is_state("sensor.period_of_day", "night") %}
            25
          {% elif is_state("sensor.period_of_day", "dusk") %}
            50
          {% else %}
            255
          {% endif %}
    - service: notify.pushover
      data_template: 
        message: "Period of day: {{ states.sensor.period_of_day }}, brightness: {{light.closets.brightness}}"
      data: 
        title: "Closet lights"

Delete the space between the % and } in {% else % }
Has to be like this: {% else %}

Thanks, yes actually saw that not too long after posting and removed, but it didn’t help I’m afraid.

Is it “legit” to use if/else in this way indented under brightness?

  data:
    brightness: >- 

replace to

  data_template:
    brightness: >-

I had troubles using this kind of templating, so I switched it using something like this:

{% if states.sensor.period_of_day == "dawn" %}
126
{% elif states.sensor.period_of_day == "night" %}
25
{% elif states.sensor.period_of_day == "dusk" %}
50
{% else %}
255
{% endif %}

Try this and tell me how it goes.

Actually Beduirs (now withdrawn?) post about changing data to data_template solved the issue, thanks.

argykaraz I see, don’t follow entirely without the context tho, where would you put it? under brightness as I already have?

Glad you sorted it out. I didnt notice the data template.
Yes, you just replace your template with mine under brightness, but dont even bother since it worked for you. :wink:

Thanks so much for replying though.

What does not work currently is the notification which is merely there for verifying the automation runs, but I’d like to understand…

I must be doing something wrong in the concationation of the message and the state of the sensor.

How would you format that?
For example: “Changed sensor to: state

And for extra credit, if I wanted to change it to percent, for example: 100*(value/255)

To show the state of a sensor you have to use this kind of templating:

{{states.thesensorhere.state}}

So, in your case you would go for this:

"Period of day: {{ states.sensor.period_of_day.state }}, brightness: {{states.light.closets.brightness.state}}"

As for your math template, you have to convert the sensor state to be float first and then do the math.

{{ ((states.light.closets.brightness.state|float) / 255) * 100 }}

I am not an expert in templating… On the contrary, I also struggle with it… I hope I made myself understandable and I also hope this template is the correct :stuck_out_tongue:

First part worked from the bat, I had to switch syntax style to parenthesis and use numeric state to get it to work but you certainly put me on the right path. And reminded me I should read the documentation, some trial and error and looking into the templating docs I arrived at this; which does what I expected:

message: >
  "Period of day changed to: {{ states('sensor.period_of_day') }}. Setting closets brightness to {{ ((state_attr('light.closets', 'brightness') | float / 255)*100) | round(0)}}%"

Well, You were far more insightful than me at least, IDK it was called templating so that certainly put me in the right direction, far better off then when we started this ordeal, thanks so much for your help.

You can test your templating by clicking on the left menu in HA and click the templates button on the bottom.
In there you can paste your templates and it should turn an immediate result. If something is wrong, it will mark the error!
Try it yourself.
Try pasting your code and see what happens.

  "Period of day changed to: {{ states('sensor.period_of_day') }}. Setting closets brightness to {{ ((state_attr('light.closets', 'brightness') | float / 255)*100) | round(0)}}%"

Your automation ensures the closet lights are on throughout the day and night and simply adjusts their brightness according to the period of day. What sort of closets are these that need to be illuminated 24 hours a day?

Thanks for the comment.
There’s actually a light-sensor built in them. So they light up on open/close, just wanted to add this as an additional layer to minimize disturbance when my SO opens to fetch clothes while I’m sleeping in the morning. Mainly though it’s cause our baby still sleeps in our bed.

So technically I wouldn’t have needed to turn on, only change brightness. Suggestion on improvement?

A light sensor? Perhaps you meant to say a motion sensor?

Doesn’t the automation turn on the light at the beginning of each new period of the day?

  action:
    - service: light.turn_on  # <---- Turns *on* light to a specified brightness
      entity_id: light.closets
      data:
        brightness: >- 

For example, when period of day changes from night to dawn, the automation turns the light on to brightness: 126. If there’s no motion detected, the light’s motion sensor will then turn the light off.

Or when commanded to turn on, does this light refuse to turn on if it detects no motion?


** EDIT **
What is the brand and model of this light? I’m interested in learning more about how it works.

I think it detects weak light, it’s off if you obscure the sensor with your thumb, but on if it’s any brigher than that, it never turns off from lack of motion at least. It’s an ikea norrfly controlled by a tradfri driver.

We rely entirely on doors/open or not to turn it on/off. It doesn’t turn on in there if the closet is closed. Needless to say it was a dumb light I’d need a more elaborate automation. So yeah if I actually in any way turned the light off with the doors open then they’d turn on at new phase of day, but that doesnt happen.

How would I by the way ONLY change the brightness, what’s the service for that?

Thanks for the explanation and the model name. I think I understand how it works now. You’re right, it’s a light sensor that turns the light on when it detects even low levels of ambient light. Within a dark closet, the sensor ensures the light is off.

So even if you send it a command to turn on, it won’t if the light level is too low. That’s perfect for the given application (closet lighting).

To my knowledge, the only light services are light.turn_on, light.turn_off, and light.toggle.

Hm, tried to use your solution and all I get is “Invalid config for [automation]: [data_template] is an invalid option for [automation]. Check: automation->data_template”

- id: '1579121918984'
  alias: Bathroom light on when motion is detected
  description: ''
  trigger:
  - device_id: f6cc3037fbff4755acecd446a3d127bc
    domain: binary_sensor
    entity_id: binary_sensor.aqara_body_sensor_occupancy
    platform: device
    type: motion
  condition: []
  action:
  - entity_id: light.tradfri_bulb_light
    device_id: 82a6b5963a144b74800cdefd9af6ead1
    domain: light
    type: turn_on
    data_template:
        brightness_pct: >- 
          {% if is_state("sensor.period_of_day", "dawn") %}
            100
          {% elif is_state("sensor.period_of_day", "night") %}
            25
          {% elif is_state("sensor.period_of_day", "dusk") %}
            50
          {% else %}
            100
          {% endif %}

What seems to be wrong? Thanks!

Your indentation. The trigger: and action: sections needs two spaces added; alternatively remove the hyphens.

That’s weird because this is my original automation (only for turning the light on), and it’s been working for a year without indentations. I tried to indentate but the error is still the same. Thanks!

- id: '1579121918984'
  alias: Turn the light on when motion is detected
  description: ''
  trigger:
  - device_id: f6cc3037fbff4755acecd446a3d127bc
    domain: binary_sensor
    entity_id: binary_sensor.aqara_body_sensor_occupancy
    platform: device
    type: motion
  condition: []
  action:
  - device_id: 82a6b5963a144b74800cdefd9af6ead1
    domain: light
    entity_id: light.tradfri_bulb_light
    type: turn_on

Hmm, that’s odd then. I’m not familiar with device automations: I’d use something like:

action:
  - service: light.turn_on
    data_template:
      entity_id: light.tradfri_bulb_light
      brightness_pct: >
      # etc

I assume you have set up the sensor.period_of_day? (reference)