Toggle automation enable using state trigger not working?

I’m trying to write an automation to toggle the enabled state of other automations based on the state change of an input_boolean variable.

- alias: "Manual lights: Toggle automations for automatic lights based on override mode"
  initial_state: "on"
  trigger:
    platform: state
    entity_id: input_boolean.override_automatic_lights
  action:
    - service_template: automation.turn_{{trigger.to_state.state}}
      data:
        entity_id: automation.automatic_lights_turn_on_lights_when_dark_mode
    - service_template: automation.turn_{{trigger.to_state.state}}
      data:
        entity_id: automation.automatic_lights_turn_off_lights_when_not_dark_mode

The log shows me that this automation is triggered as it should, but it doesn’t enable/disable the other automations. If I replace with these two instead it works

- alias: "Manual lights: Disable automations for automatic lights when override mode is enabled"
  initial_state: "on"
  trigger:
    platform: state
    entity_id: input_boolean.override_automatic_lights
    from: "off"
    to: "on"
  action:
    - service: automation.turn_off
      entity_id: automation.automatic_lights_turn_on_lights_when_dark_mode
    - service: automation.turn_off
      entity_id: automation.automatic_lights_turn_off_lights_when_not_dark_mode

- alias: "Manual lights: Enable automations for automatic lights when override mode is disabled"
  initial_state: "on"
  trigger:
    platform: state
    entity_id: input_boolean.override_automatic_lights
    from: "on"
    to: "off"
  action:
    - service: automation.turn_on
      entity_id: automation.automatic_lights_turn_on_lights_when_dark_mode
    - service: automation.turn_on
      entity_id: automation.automatic_lights_turn_off_lights_when_not_dark_mode

It would be nice to be able to toggle the state instead.
Any ideas?

Single line templates need to be enclosed in quotes.

1 Like

Thanks for your reply.

I tried to wrap in quotes, use the “>” syntax for multiple lines and I read that data_template should be used instead of data but still not working.

I have another similar automation that works:

- alias: "Manual lights: Toggle upstairs lights with remote (override)"
  initial_state: "on"
  trigger:
    platform: event
    event_type: button_pressed
    event_data:
      entity_id: switch.nexa_pet_3_channel_1
  action:
    - service_template: light.turn_{{ trigger.event.data.state }}
      data:
        entity_id: light.upstairs
    - service: input_boolean.turn_on
      entity_id: input_boolean.override_automatic_lights

The same syntax with

automation.turn_{{trigger.to_state.state}}

doesn’t seem to do anything.

No errors, nothing in the logs as far as I can see at least.

I suspect you may not have reloaded/restarted at some point. I also recommend either enclosing the template in quotes, or using multiline YAML, even if, in your case, it’s probably not strictly required. Also, you can change both automations with one service call:

- alias: "Manual lights: Toggle automations for automatic lights based on override mode"
  initial_state: "on"
  trigger:
    platform: state
    entity_id: input_boolean.override_automatic_lights
  action:
    - service_template: "automation.turn_{{trigger.to_state.state}}"
      entity_id:
        - automation.automatic_lights_turn_on_lights_when_dark_mode
        - automation.automatic_lights_turn_off_lights_when_not_dark_mode

Thanks for you tips! Always nice to be able to simplify with a single call.

Got the automation running finally, turns out I had it all wrong with the “trigger.to_state” vs “trigger.from_state” so the toggle never happened because I told it to stay on! (doh)