Merge 2 Automations with oposite function

I can not run these 2 devices at the same time, so whenever switch.2 is on, climate.styrbordfrostvakt has to stay off. And since its very cold, climate.styrbordfrostvakt has to be on whenever switch.2 is off.

I use 2 Automations to do this, how do I merge them to one?

alias: Styrbord Frostvakt AV når Varmebehov på
description: ''
trigger:
  - platform: state
    entity_id: switch.2
    from: 'off'
    to: 'on'
condition: []
action:
  - service: climate.turn_off
    target:
      entity_id: climate.styrbordfrostvakt
mode: single
alias: Styrbord Frostvakt på når Varmebehov av
description: ''
trigger:
  - platform: state
    entity_id: switch.2
    from: 'on'
    to: 'off'
condition: []
action:
  - service: climate.turn_on
    target:
      entity_id: climate.styrbordfrostvakt
mode: single

Here’s one way:

trigger:
  - platform: state
    entity_id: switch.2
action:
  - service: "climate.turn_{{trigger.to_state.state}}"
    target:
      entity_id: climate.styrbordfrostvakt
1 Like

Will that code set climate.styrbordfrostvakt to the opposite of switch.2?
So if switch.2 is off the other goes on, and opposite?

Oops. No it will not. This will though:

trigger:
  - platform: state
    entity_id: switch.2
action:
  - service: "climate.turn_{{trigger.from_state.state}}"
    target:
      entity_id: climate.styrbordfrostvakt
1 Like

Testing :slight_smile:

Works :slight_smile: Thanks!

1 Like