Set up a switch that reflects the status of another sensor?

Hi All,

I need some help setting up a virtual switch that controls a relay but reflects the status of another sensor. I have a momentary relay that opens or closes a garage door and a contact sensor that returns the state (open or closed) of the door. Currently I have a virtual switch that reflects the status of the sensor but does not toggle the relay. Thanks for your help here and let me know if you need any more info.

If I understand it correct you need something like the following, let me know if it is what you’re looking for:

type: button
entity: binary_sensor.your_contact_sensor
name: Garagedoor
show_state: true
show_name: true
show_icon: true
tap_action:
  action: call-service
  service: switch.toggle
  target:
    entity_id: switch.your_momentary_toggle_switch

Sounds like you need a template switch:

The value template should copy the sensor, the commands should operate the relay.

2 Likes

Thanks for the feedback! I think this is close to what I am looking for. Is there a way to edit the YAML of a virtual entity? I wanted to do this on a dashboard originally, but now I think it would be better as an entity so I can use it in third party apps.

I’m pretty sure this is what I need. I’ll try this out now and let you know how it works.

Thanks for the tip! Can you help me with the implementation? I have added the below into my configuration.yaml:

switch:
  - platform: template
    switches:
      kgd:
        friendly_name: "KGD"
        value_template: "{{ is_state_attr('binary_sensor.k_garage_door_contact', 'sensor_state', 'on') }}"
        turn_on:
          service: switch.toggle
          target:
            entity_id: switch.k
        turn_off:
          service: switch.toggle
          target:
            entity_id: switch.k

“switch.k” is the relay and “binary_sensor.k_garage_door_contact” is the contact sensor. I don’t see any changes to the switch, so either this isn’t configured properly or I am looking in the wrong place. Thanks for your inputs!

cover:
   #
   # Garage Gate Cover
   - platform:template
     covers:
       garage_door:
         device_class:garage
         friendly_name: "Garage Portal"
         value_template: "{{ is_state('binary_sensor.portail_voiture', 'on') }}"
         open_cover:
           service: light.turn_on
           entity_id: light.switch_portail_garage_light
         close_cover:
           service: light.turn_on
           entity_id: light.switch_portail_garage_light
         stop_cover:
           service: light.turn_on
           entity_id: light.switch_portail_garage_light

@Guerardi beat me to it: a state of a sensor and an attribute are not the same thing. You are trying to get the state as if it were an attribute. For state it is just is_state. Attributes are used less and less. You might need it for instance to get to the color or brightness of a light, while the state of the light is on or off.

So the value template should read (pretty much as the example in the documentation):

    value_template: "{{ is_state('binary_sensor.k_garage_door_contact', 'on') }}"

What @Guerardi also changed: instead of a switch you could implement a cover entity, which is a more true representation of the device you want to emulate. So while not an exact implementation of your question, it is an even better way to solve your original problem. There are some errors in his example though, a space is missing for the device class, and all operations do the same thing: turn on a light. But the idea is right.

1 Like

That got it. Thanks for the help!

1 Like

Super !!! Merci :wink: