Hello Gents,
I would like to have a single switch which when clicked it trigger a specific switch and when clicked again triggers another switch…
Is this doable ? I need it to toggle between two switches entities like on/off
Hello Gents,
I would like to have a single switch which when clicked it trigger a specific switch and when clicked again triggers another switch…
Is this doable ? I need it to toggle between two switches entities like on/off
Call this script from a button tap action:
toggle_two_switches:
sequence:
- choose:
- conditions:
- condition: state
entity_id: switch.switch_1
state: 'on'
sequence:
- service: switch.turn_off
entity_id: switch.switch_1
- service: switch.turn_on
entity_id: switch.switch_2
default:
- service: switch.turn_on
entity_id: switch.switch_1
- service: switch.turn_off
entity_id: switch.switch_2
See here for more: https://www.home-assistant.io/docs/scripts#choose-a-group-of-actions
A variation of tom_I’s approach, employing templates:
toggle_two_switches:
sequence:
- variables:
first_is_on: "{{ is_state('switch.switch_1', 'on') }}"
- service: switch.turn_on
target:
entity_id: "switch.{{ 'switch_2' if first_is_on else 'switch_1' }}"
- service: switch.turn_off
target:
entity_id: "switch.{{ 'switch_1' if first_is_on else 'switch_2' }}"
You are going to have to explain better what you mean by that.
What if i want to toggle between two automations
For example the first automation is set to change the scene to movie time and when clicked again change to day time lighting
So you want to enable or disable two automations with the toggle button?
You just replace the switch entity ids with automation ids and use the homeassistant.turn_on
/ off
service instead of switch.turn_on
/ off
. Like this:
toggle_two_automations:
sequence:
- variables:
day_is_on: "{{ is_state('automation.day_time_lighting', 'on') }}"
- service: homeassistant.turn_on
target:
entity_id: "automation.{{ 'movie_lighting' if day_is_on else 'day_time_lighting' }}"
- service: homeassistant.turn_off
target:
entity_id: "automation.{{ 'day_time_lighting' if day_is_on else 'movie_lighting' }}"
Wow that was helpful, but i guess i did something wrong. I put the below in a script, Any idea why this error is showing "Message malformed: extra keys not allowed @ data[‘toggle_two_automations’]
"
toggle_two_automations:
sequence:
- variables:
day_is_on: '{{ is_state(''automation.ha_automation_daylight'', ''on'') }}'
- service: homeassistant.turn_on
target:
entity_id: >-
automation.{{ 'ha_automation_daylight' if day_is_on else 'netflix_movies_automation'
}}
- service: homeassistant.turn_off
target:
entity_id: >-
automation.{{ 'netflix_movies_automation' if day_is_on else 'ha_automation_daylight'
}}
mode: single
What is the full error text?
Quotes in the variable and the targets are different from the example given above ?
As a first, indent mode to be on the same level as sequence
Please can you check the updated below:
*Message malformed: extra keys not allowed @ data[‘toggle_automation’]
toggle_automation:
sequence:
- variables:
day_is_on: '{{ is_state(''automation.ha_automation_daylight'', ''on'') }}'
- service: homeassistant.turn_on
target:
entity_id: >-
automation.{{ 'ha_automation_daylight' if day_is_on else
'netflix_movies_automation' }}
- service: homeassistant.turn_off
target:
entity_id: >-
automation.{{ 'netflix_movies_automation' if day_is_on else
'movie_lighting' }}
mode: single
Can you try this YAML?
alias: Toggle Automation
sequence:
- variables:
day_is_on: >-
{{ is_state('automation.ha_automation_daylight', 'on') }}
- service: homeassistant.turn_on
target:
entity_id: >-
automation.{{ 'ha_automation_daylight' if day_is_on else
'netflix_movies_automation' }}
- service: homeassistant.turn_off
target:
entity_id: >-
automation.{{ 'netflix_movies_automation' if day_is_on else
'movie_lighting' }}
mode: single
Normally I would jump in to help but I am allergic to the concept of enabling/disabling automations as a normal practice. Use a better trigger or a condition in the automation to control when it should/shouldn’t execute.
This code was executed without errors and the reached final code was able trigger status successfully (Icons color light changes sucessfully) but no action occurs… the automations were not triggered.
The last YAML code provided by tom_l is to toggle automation on/off.
Does the automation not turning off and on as you expected?
Unfortunately it was not triggering the automation, the button just is toggling
That’s not what I understood you to be asking. I thought you meant this:
Enabling and triggering are two different things.
Also if you are triggering automations manually you should be using scripts instead (they’re just automations without triggers).
how can I update the below script to trigger automations
alias: Toggle Automation
sequence:
- variables:
day_is_on: '{{ is_state(''automation.ha_automation_daylight'', ''on'') }}'
- service: homeassistant.turn_on
target:
entity_id: >-
automation.{{ 'netflix_movies_automation' if day_is_on else
'ha_automation_daylight' }}
- service: homeassistant.turn_off
target:
entity_id: >-
automation.{{ 'ha_automation_daylight' if day_is_on else
'netflix_movies_automation' }}
mode: single