Invert {{ trigger.to_state.state }}?

I’m trying to create an automation which turns off switch if binary sensor is on and vice versa. Command in title is “state follower”. But, can be changed to be inverted state follower?

this is what i have; i’d like fan to turn off if light gets on and vice versa:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.light_cellar
condition: []
action:
  - service: switch.turn_{{ trigger.to_state.state }}
    target:
      entity_id: switch.fan_cellar

Things like “trigger.to_not_state” … don’t work…
Sure, i could solve it with if-else. But, i’d like to keep it as simple as possible.

- service: switch.turn_{{ 'on' if trigger.to_state.state == 'off' else 'off' }}

Why not just use

- service: switch.turn_{{ trigger.from_state.state }}

You may want to handle unavailable/unknown states though. Perhaps with:

trigger:
  - platform: state
    entity_id:
      - binary_sensor.light_cellar
    not_from:
      - unavailable
      - unknown
condition: []
action:
  - service: switch.turn_{{ trigger.from_state.state }}
    target:
      entity_id: switch.fan_cellar

If you really want to use the to_state I was able to get rid of the if-else, but didn’t succeed with “keep it as simple as possible” :grin:

- service: switch.turn_{{ ( ['on','off'] | sort(reverse=( trigger.to_state.state == 'off') ) )[0] }}

Just realized that this works and isn’t very complicated

- service: switch.turn_{{ ['on','off'][trigger.to_state.state == 'on'] }}

Try this one?
Should work.

trigger:
  - platform: state
    entity_id: binary_sensor.light_cellar
    from: ["off", "on"]
    to: ["on", "off"]
action:
  - service: switch.turn_{{ trigger.from_state.state }}
    target:
      entity_id: switch.fan_cellar

Wow, many thanks, guys! Sorry, i’ve been away all day, i’ll test these examples tomorrow.

EDIT: all above solutions work. Thanks again!