Trying to figure out how to work multiple conditions and multiple lights in 1 automation

I am working from this base automation:

- alias: 'Single Click 21 to toggle stair lights'
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001f3de21
      click_type: single
  action:
    service: light.toggle
    data:
      entity_id: light.stair_top,light.stair_bottom

I would like a set of conditions in an “or” fashion based on time. Between 22:00 and 06:00 I would like the click to turn the lights on a 25% and between 06:00:01 and 21:59:59, I would like to turn the lights on at 100%. The problem I am trying to solve is how in the world do you build a condition that actually causes an action.

well, i don’t think you need any conditions. Just a data_template that places a variable brightness:

This is the doc for templating:

Your action would look something like this:

action:
  service: light.toggle
  data_template:
    entity_id: light.stair_top,light.stair_bottom
    brightness: >
      {% if ..... %}
      64 # this is 25% of 255
      {% else %}
      255
      {% endif %}

Your if statement would compare time with your time range that you use for 25% light.

1 Like

Petro is right, just use the data_template:

    action:
      - service: light.toggle
        data_template:
          entity_id: light.stair_top,light.stair_bottom
          brightness: '{% if now().hour < 6 %} 64 {% else %} 255 {% endif %}'

I really stink at templates, docs just have not made sense to me yet. It look like this one will set a low brightness level from midnight to 6AM? So from say 10PM to midnight they will still come on at full brightness?

So, can I do an if <6 64, else >22 64, else 255?

Try this:

action:
  service: light.toggle
  data_template:
    entity_id: light.stair_top,light.stair_bottom
    brightness: >
      {%- if now().hour >= 6 and now().hour <=21 %}
        255 
      {% else %}
        64
      {% endif %}

What this says if the hour now is greater than or equal to 6 and less than or equal to 21 than brightness equals 255. Any other hour which would be 22, 23 and 0 to 5 than brightness equals 64.

So, I have been trying to get this to function and not having great luck. What is this output trying to tell me?

Code:

- alias: 'Single Click 21 to toggle stair lights'
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001f3de21
      click_type: single
  action:
  - service: light.toggle
    data_template:
      entity_id: light.stair_top,light.stair_bottom
      brightness: >
        {%- if now().hour >= 6 and now().hour <=21 %}
          255 
        {% else %}
          64
        {% endif %}

Errors:

2018-04-23 23:41:45 INFO (MainThread) [homeassistant.components.automation] Executing Single Click 21 to toggle stair lights
2018-04-23 23:41:45 ERROR (MainThread) [homeassistant.core] Invalid service data for light.toggle: extra keys not allowed @ data['brightness']. Got '64'
2018-04-23 23:41:48 INFO (MainThread) [homeassistant.components.automation] Executing Single Click 21 to toggle stair lights
2018-04-23 23:41:48 ERROR (MainThread) [homeassistant.core] Invalid service data for light.toggle: extra keys not allowed @ data['brightness']. Got '64'
2018-04-23 23:41:51 INFO (MainThread) [homeassistant.components.automation] Executing Double Click 21 to turn off all lights

64 is a string use:

{{ 64 }}

Not sure why it isn’t converting to int though. It should naturally.

So… the error is indicating that the 64 is being presented as alpha-numeric and not an integer? I will test this tonight.

Thanks for the help!

I believe so, its complaining about the value, not about the shape of the json object. So that means everythings good until the value is getting set. Using {{}} executes the command/variable/expression, so you are executing 64 which should be interpreted as an integer.

So… I adjusted the automation to this:

- alias: 'Single Click 21 to toggle stair lights'
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001f3de21
      click_type: single
  action:
  - service: light.toggle
    data_template:
      entity_id: light.stair_top,light.stair_bottom
      brightness: >
        {%- if now().hour >= 6 and now().hour <=21 %}
          {{254}} 
        {% else %}
          {{64}}
        {% endif %}

Error:

2018-04-24 20:25:12 INFO (MainThread) [homeassistant.components.automation] Executing Single Click 21 to toggle stair lights
2018-04-24 20:25:12 ERROR (MainThread) [homeassistant.core] Invalid service data for light.toggle: extra keys not allowed @ data['brightness']. Got '254'
2018-04-24 20:25:12 DEBUG (MainThread) [homeassistant.components.websocket_api] WS 1874338352: Sending {'id': 2, 'type': 'event', 'event': {'event_type': 'state_changed', 'data': {'entity_id': 'automation.single_click_21_to_toggle_stair_lights', 'old_state': <state automation.single_click_21_to_toggle_stair_lights=on; last_triggered=2018-04-24T20:24:45.303608-05:00, friendly_name=Single Click 21 to toggle stair lights @ 2018-04-24T20:17:46.973016-05:00>, 'new_state': <state automation.single_click_21_to_toggle_stair_lights=on; last_triggered=2018-04-24T20:25:12.261897-05:00, friendly_name=Single Click 21 to toggle stair lights @ 2018-04-24T20:17:46.973016-05:00>}, 'origin': 'LOCAL', 'time_fired': datetime.datetime(2018, 4, 25, 1, 25, 12, 262701, tzinfo=<UTC>)}}

I find it’s handy using the template editor in the dev tools. Pasting your template action gave me this: (Note I removed the > after brightness) I’ve found that useful in all kinds of ways when troubleshooting.

Also, you have 2 entity id’s separated with a comma? I thought you would have them on separate lines…

- alias: 'Single Click 21 to toggle stair lights'
  trigger:
    platform: event
    event_type: click
    event_data:
      entity_id: binary_sensor.switch_158d0001f3de21
      click_type: single
  action:
  - service: light.toggle
    data_template:
      entity_id: light.stair_top
      brightness: >
        {%- if now().hour >= 6 and now().hour <=21 %}
          {{254}} 
        {% else %}
          {{64}}
        {% endif %}
      entity_id: light.stair_bottom
      brightness: >
        {%- if now().hour >= 6 and now().hour <=21 %}
          {{254}} 
        {% else %}
          {{64}}
        {% endif %}

Something like that but I’m no expert. Try and get it to work with just one light first…

So, I took your advice and tested it in the template editor. When I remove the “>” in question it will not pass yaml inspection. It appears that “>” tells the editor that this is a multi-line template. So, it appears it has to stay as the automation will not load without it.

Error:

missed comma between flow collection entries at line 66, column 12:
              {%- if now().hour >= 6 and now(). ... 
               ^

So, I did simplify it down to one light. HA docs do support the “,” separated list of entity_id’s and I use it other places… But, I like the keep it simple stupid idea.

But, no change same error same result.

I really think either it is not viewing the 64 or 254 as an integer.

You can see the result to the right in mine - it’s seeing 254 perfectly. I also had the > in there and that worked as well.

Not disagreeing… just confused by the service data error.

does the light.toggle service support brightness???

according to the docs it doesn’t

Ah, just tried homeassistant.toggle, appears not to either. Well crap!

So, can I turn this into a turn_on and turn_off in 1 automation? I can use brightness in a template there.

Shouldn’t it be light.turn_on, since you are trying to turn the light on to a certain brightness?

Although I may be wrong, to me light.toggle, means turn it ON or OFF.

I don’t think you can have 2 different services in one automation.
You could use a separate automation and use a condition to change the brightness when the light turns on according to the times you specified.

You could also go over to Discord and see if Dale is around as he is an absolute genius at automations. I had 8 automations last week he was able to help me condense into 2. Genius!

Reading the docs helps… who knew LOL!

1 Like

The toggle command works as I would expect, if I press the button the light turns on and if I press the button again it turns off. The little Mi switch sits at “0” and when pressed presents a “1” for a short bit and the returns to “0”.

This results in a state transition of each light when the button is pressed.