Automation if else logic

Hey all,
I Have an automation that listens to a timer to finish and based on a brightness value set in an input_number i want to turn off a light or restore a previous set brightness value. the problem is that the turn_off action doesn’t work because turn_off doesn’t support brightness value. This needs to be conditional but no idea how to fix this on this level. Any suggestion on to fix this.

alias: "Turn lights to previous state when front yard light timer ends"
initial_state: True

trigger:
  - platform: event
    event_type: timer.finished
    event_data:
      entity_id: timer.front_door_outside

action:
  - service_template: >
      {% if states.input_number.front_yard_light_brightness.state | int > 0 %}
        light.turn_on
      {% else %}
        light.turn_off
      {% endif %}
    data_template:
      entity_id: group.front_garden_lights
      brightness: "{{ states.input_number.front_yard_light_brightness.state | int }}"
1 Like

I’d make two scripts, one to turn on the lights at the brightness specified and one to turn them off; using the turn_off call. Test them to make sure they work.

Then use those in your service_template call to determine which one to call. You will not need a data template.

Thats the easiest way when you have a service template in which the actions take different data parameters.

action:
  - service_template: >
      {% if states.input_number.front_yard_light_brightness.state | int > 0 %}
        script.garden_lights_on
      {% else %}
        script.garden_lights_off
      {% endif %}
2 Likes

I may be wrong about this but isn’t the problem here due to the fact you’re calling the wrong service? You’re using light.turn_off to act on group.front_garden_lights which is not a member of the light domain.

For groups, use homeassistant.turn_on and homeassistant.turn_off. There’s an example in the documentation for Service Calls:


EDIT

Bla bla bla … no.

if that group only has light devices its fine. The challenge is the data_template which contains the brightness is not valid for a turn_off call.

It would be nice if the turn_on/turn_off calls would take the same parameters even if on the turn_off they are ignored (for example brightness)

The light.turn_off and light.turn_on services can accept groups in its entity_id parameter. The group will be expanded, recursively, until all the non-group entity_id’s are found.

Indeed I was! :slight_smile:

1 Like

Let’s take a step back. You say you’ve saved the brightness, and now you’re restoring it after a timer expires. If you’re interested, I wrote a python_script that can save the state of lights and/or switches (and accepts groups), and can later restore them. You can check it out here. You may find this makes your automations easier.

What happens when I restart Home Assistant and the state isn’t saved yet and I then call the restore script.
When not found will a lights turn_off is called then?

That’s a good question. This script works by saving data to the state machine, which itself provides no persistence across restarts. So if you were to call the script to restore after a restart, but before any states were saved, there would be nothing to retrieve, and hence it would have no effect. So, if you need persistence across restarts, this script would not suit your needs.

I made some changes to my scripts and automation. Your script works great. Maybe one feature request is to add an extra option when you save the data is something like ignore_if_already_saved.

One of my scenarios is to lighten up the front yard lights for 2 minutes when somebody rings the doorbell. I now have to do a check if the data is already stored and then don’t store it again when someone keeps pressing the doorbell. Otherwise the lighten up data is stored.

Good idea. I could add an overwrite option that defaults to true. If set to false, then if any entities exist in the state machine with the domain name equal to the store_name option, then skip saving.

See issue #91.

This could drastically reduce some logic and conditions within scripts and/or automations.

If you’d like to give it a try, a beta version is available. The new parameter – overwrite – only applies to saving, and defaults to true. If you don’t want already saved values to be replaced, then set it to false.

Did you get a chance to try the beta version with the feature you requested?

I was busy the last couple of days. Will try it this week and give you feedback.

1 Like

it’s still on my todo list but very busy with other stuff lately

No problem. I went ahead and released it as v1.2.0 on the 4th.