Templating automations into one

I’d like to use template in order to merge these automations into one.
Basically, when i turn off the desktop the switch goes off (and the opposite if PC is turned on).

Thanks

- id: SpegniLuceStudio
  alias: Spegni luce studio se PC off
  trigger:
    entity_id: device_tracker.saradesktop
    platform: state
    from: home
    to: not_home
  action:
    - service: switch.turn_off
      data:
        entity_id: switch.plug_158d0001bc2dbe


- id: AccendiLuceStudio
  alias: Accendi luce studio se PC on
  trigger:
    entity_id: device_tracker.saradesktop
    platform: state
    from: not_home
    to: home
  action:
    - service: switch.turn_on
      data:
        entity_id: switch.plug_158d0001bc2dbe
    - id: LuceStudio
      alias: Luce studio se PC
      trigger:
        platform: state
        entity_id: device_tracker.saradesktop
      action:
        service_template: >
          {% if is_state('device_tracker.saradesktop' , 'home') %} switch.turn_on
          {% else %} switch.turn_off {% endif %} 
        entity_id: switch.plug_158d0001bc2dbe

(I guessed on the id and alias)

1 Like