Toggle an Input_boolean as an action

I think I am right in saying I can trigger an automation on any change in the state of an input Boolean with:

trigger:
  platform: state
  entity_id: input_boolean.my_boolean

But can I toggle the input_boolean in an action, i.e if it is ‘On’, turn it ‘Off’ and if it is ‘Off’ turn it ‘On’?

Try something like this:

trigger:
  platform: state
  entity_id: input_boolean.my_boolean
action:
  service_template: >
    {% if states.input_boolean.state == "on" %}
		input_boolean.turn_off
	{% else %}
		input_boolean.turn_on
	{% endif %}
  data:
    entity_id: input_boolean.my_boolean

That’s incredibly overcomplicating a simple thing…

action:
  service: homeassistant.toggle
  entity_id: input_boolean.YOUR_INPUT_BOOLEAN
5 Likes

OP - docs are your friend…

2 Likes

Thanks to both of you.

@anon43302295 Yes, beautifully simple but strangely, on reflection the solution by @pplucky might actually fit my application better.

And yes the docs are my friend but I sometimes have trouble getting those friends to come out to play. I don’t find the docs organisation always intuitive and It did not occur to me to look at the Input_boolean documentation to see how to use it in an automation. I learned more than one thing from this question.

I think the Automation documentation should have a note (near the top) to suggest looking at the docs for other components directly for further information.

1 Like

I am trying something similar.
But in my case I want to run one of two scripts relating to a condition of an input_boolean.:

- type: custom:button-card
                entity: input_boolean.multiroom
                state:
                  - value: 'off'
                    icon: mdi:spotify
                    color: green
                  - value: 'on'
                    icon: mdi:radio
                    color: orange
                size: 80px
                tap_action:
                  action: call-service
                  service: >
                    {% if states.input_boolean.multiroom == 'on' %}
                      script.multiroom_radio
                    {% else %}
                      script.multiroom_spotify
                    {% endif %}
                  service_data: {}
                  target: {}
                show_state: false
                show_name: true
                name: Quelle
                hold_action:
                  action: none

But with this code I get an error message.
Can someone help me?