Automations in release 0.113

There are new automation options available and I’m trying to combine multiple pre-0.113 automations, but can’t get them to work. I have three lights and the automation has to do this:

  1. If one of the lights are turned on, the other two lights turn on.
  2. If one of the lights are turned off, the other two lights turn off.

The configuration validation gives me a “Configuration valid!”, but the GUI automation editor gives me error "Unsupported action: " and displays the yaml. The automation doesn’t turn on or off any lights. What am I doing wrong here?

- id: auto_switch_kitchen_on_helper
  alias: Switch Kitchen On Helper
  mode: single
  trigger:
    - platform: state
      entity_id: 
        - light.kitchen_counter
        - light.kitchen
        - light.kitchen_extractor
  action:
    - choose:
      - conditions:
          - condition: state
            entity_id: 
              - light.kitchen_counter
              - light.kitchen
              - light.kitchen_extractor
            state: 'on'
        sequence:
          - service: light.turn_on
            entity_id: 
              - light.kitchen_counter
              - light.kitchen
              - light.kitchen_extractor
      - conditions:
          - condition: state
            entity_id: 
              - light.kitchen_counter
              - light.kitchen
              - light.kitchen_extractor
            state: 'off'
        sequence:
          - service: light.turn_off
            entity_id:
              - light.kitchen_counter
              - light.kitchen
              - light.kitchen_extractor

I can’t comment on the Automation Editor because I don’t use it.

As for the automation you created, take a second look at each condition you created. According to the documentation, the first condition expects the state of all three lights to be on before it calls the turn_on service. That’s probably not how you thought it works. You thought if any one light is on then turn them all on.

Even if you fix that there remains a little problem with this approach because the moment it turns on the other lights, their state-changes will trigger this automation again.

1 Like

So three things.

First, without using to in the state trigger your automation will probably trigger too often, because any attribute change (such as brightness) will cause it to trigger, too. I’d recommend:

  trigger:
    - platform: state
      entity_id: 
        - light.kitchen_counter
        - light.kitchen
        - light.kitchen_extractor
      to:
        - 'on'
        - 'off'

That way it will only trigger when one of the lights actually turns on or off.

Second, you need to use the trigger variable in your actions to determine what happened (i.e., if a light turned on or off.) Just looking at the current state of the lights can’t tell you that unambiguously.

Third, this is another case of using the choose action when a simple service_template will do.

This is what I suggest:

- id: auto_switch_kitchen_on_helper
  alias: Switch Kitchen On Helper
  mode: parallel
  trigger:
    - platform: state
      entity_id: 
        - light.kitchen_counter
        - light.kitchen
        - light.kitchen_extractor
      to:
        - 'on'
        - 'off'
  action:
    - service_template: "light.turn_{{ trigger.to_state.state }}"
      entity_id: 
        - light.kitchen_counter
        - light.kitchen
        - light.kitchen_extractor

As far as the automation getting re-triggered when the other lights get turned on (or off), that’s true. However, since the lights will already be on (or off) nothing will change so it will stop there. (And to avoid any “already running” warnings, I suggested using parallel mode here.)

3 Likes

I was thinking along the lines that if you leave it in the default mode (single) the subsequent call to execute the already-running script would simply be blocked (since there’s no real need to run the same script concurrently). The ‘little problem’ is just the warning message posted to the log each time this happens.

Yep, but since most people don’t like warnings, and since running it in parallel mode doesn’t hurt anything in this case, as a work around…

Aah yes… so it AND logic, and not OR logic… got it.
And you are right, all the lights keep being triggered and can’t turn them off any more.

Changed it to the code suggested by pnpbruckner and it works as described. Thanks @pnbruckner and @123

1 Like

I guess I’m in the minority. Given a choice between warnings in the log or having the automation run three times instead of just once (3 lights x 1 execution per light), I’ll take the warnings.

I seem to recall that, when this was in beta, there was an option that provided single mode without warnings. Any chance of this feature re-appearing in a future release?

2 Likes

Yea it would be nice to be able to change that. Since the default execution mode is single I think having the default log level for the message be warning is useful. That way if people just didn’t pay attention to the new execution mode option and ended up confused and wondering what was going on later there would be a clear log message explaining it.

But when you design around this functionality like what’s being discussed here it would be great to be able to turn that logging off or set the log level of the message to debug. There’s no reason to warn in this case, you’re explicitly relying on that behavior in your logic.

1 Like

and @CentralCommand

Yes, it’s on my list of things to do. Hopefully I’ll come up with an acceptable way in the next few releases.

And, BTW, I’m in total agreement. I like the warning by default, but when you know you want an automation or script to work that way, then I hate a warning I can’t filter. (It wouldn’t be so bad if warnings could be filtered out via logger, but they can’t. And it’s even possible now to set a logger level per script or automation, since their log names now include their object_id’s. E.g., homeassistant.components.script.my_script.)

There were several approaches discussed before 0.113.0 was released, but no consensus, so it was decided to leave it as is for now.

My preferred choice is to be able to set the log level when the “max” is exceeded, which would apply to single, parallel & queued (where the “max” for single is effectively 1. It, of course, does not affect restart mode since it’s impossible to have more than one in that mode.) This would allow setting the log level on a per script/automation basis where the default is warning. I think besides the normal levels, another choice, such as none, would be good, too.

5 Likes

That’s the reason I asked in the .112 release thread for the ability to make both the entity_id and states of multiple conditions as either “and” or “or”.

like possibly:

condition:
  platform: state
  entity_id: or
    entity_ids:
      - device_tracker.frenck
      - device_tracker.daphne
  state: and
    states:
      - 'home'
      - 'work'

But unfortunately Frenck completely shot that down immediately and wasn’t even interested in considering it…

hey Phil,

can we add a for: to these to: states individually? I have several automations which now have listed all entities twice, eg once for

to: 'on'
for:
  seconds: 10

and once for

to: 'off'

would be really nice if we could do

      to:
        - 'on'
          for: 
            seconds: 10
        - 'off'

full example:

  - alias: Sense Active change
    id: Sense Active change
    trigger:
      - platform: state
        entity_id:
          - binary_sensor.espresso_keuken_active_threshold
          - binary_sensor.quooker_keuken_active_threshold
          - binary_sensor.koelkast_keuken_active_threshold
          - binary_sensor.cv_garage_active_threshold
          etc
        to: 'on'
        for:
          seconds: 10
      - platform: state
        entity_id:
          - binary_sensor.espresso_keuken_active_threshold
          - binary_sensor.quooker_keuken_active_threshold
          - binary_sensor.koelkast_keuken_active_threshold
          - binary_sensor.cv_garage_active_threshold
          etc
        to: 'off'

https://community.home-assistant.io/c/feature-requests?order=votes

I take that as a no for now…
will try to formulate a FR.

same would probably go for the use of {{trigger.to_state.state_with_unit}} ? I’ve recently found the need for that, tried it without getting errors logged, but in that field, simply nothing is displayed.

Would have hoped since even the developer-tools/template show this as example:

For loop example:
{% for state in states.sensor -%}
  {%- if loop.first %}The {% elif loop.last %} and the {% else %}, the {% endif -%}
  {{ state.name | lower }} is {{state.state_with_unit}}
{%- endfor %}.

it would have been an option for the trigger.states…

Hope it can be realized, would simplify things even more.

1 Like

BTW, you can use YAML anchors to make this less error prone:

  - alias: Sense Active change
    id: Sense Active change
    trigger:
      - platform: state
        entity_id: &sense_active_change_entities
          - binary_sensor.espresso_keuken_active_threshold
          - binary_sensor.quooker_keuken_active_threshold
          - binary_sensor.koelkast_keuken_active_threshold
          - binary_sensor.cv_garage_active_threshold
          etc
        to: 'on'
        for:
          seconds: 10
      - platform: state
        entity_id: *sense_active_change_entities
        to: 'off'
2 Likes

Of course ! Great idea. Thanks!

1 Like

Yes it is. I use it quite a bit but reading this has prompted me to think about it a bit more…

Is it possible to make it even more flexible with a syntax that makes something like this possible?

  - alias: Sense Active change
    id: Sense Active change
    trigger:
      - platform: state
        entity_id: &sense_active_change_entities
          - binary_sensor.espresso_keuken_active_threshold
          - binary_sensor.quooker_keuken_active_threshold
          - binary_sensor.koelkast_keuken_active_threshold
          - binary_sensor.cv_garage_active_threshold
          etc
        to: 'on'
        for:
          seconds: 10
      - platform: state
        entity_id: *sense_active_change_entities
          - another.entity_id   #  <<<---
        to: 'off'

Well. Did you try ? :wink:

Learn something new everyday, didn’t think of YAML anchors… :smiley:

I did yes and the VS Code checker didn’t like it.
So I wondered if there was another syntax?

There may be a way to do that, but I can tell I wanted to do just that a while ago and I searched and tried and finally gave up.

1 Like