I have seen several posts related to combining sensors and switches, have read the docs, particularly on templating, but still would need some guidance on whether this is possible and how.
I have a Garage Door for which I have a Sensor (for open/close state) which is composed of a Sonoff Mini with a Reed Switch attached to the S1 and S2 Terminals, and a a switch composed of a Sonof SV attached to the garage motors contacts.
I would like to see the sensor and switch as one combined entry, with the state changing the icon and color, and opening/closing by clicking the entry on Lovelace UI:
here is the code I use with a zwave dry relay switch to simulate the pushbutton and a zwave tilt sensor for the door position.
I use scripts to make sure the door is in the correct position before operating the switch. IOW, if I push the “open” button it makes sure the door is closed before it will work and vice versa. I’m not sure if it’s necessary but I also use those scripts for the Alexa integration too and it’s definitely necessary there since Alexa has no idea what the door postion is and it will happily “push the button” to open the door even if it’s already open thus closing the door.
Here is the code:
cover:
- platform: template
covers:
north_garage_door:
friendly_name: 'North Garage Door'
value_template: "{{ is_state('binary_sensor.garage_door_north_position_sensor', 'on') }}"
open_cover:
service: script.turn_on
entity_id: script.open_gdn
close_cover:
service: script.turn_on
entity_id: script.close_gdn
stop_cover:
service: switch.turn_on
entity_id: switch.garage_door_north_operator_switch
icon_template: "{% if not is_state('binary_sensor.garage_door_north_position_sensor', 'off') %}mdi:garage-open{% else %}mdi:garage{% endif %}"
script:
close_gdn:
alias: Close the North Garage Door
sequence:
- condition: state
entity_id: binary_sensor.garage_door_north_position_sensor
state: 'on'
- service: switch.turn_on
entity_id: switch.garage_door_north_operator_switch
open_gdn:
alias: Open the North Garage Door
sequence:
- condition: state
entity_id: binary_sensor.garage_door_north_position_sensor
state: 'off'
- service: switch.turn_on
entity_id: switch.garage_door_north_operator_switch