How to use sensor state as state for a button in lovelace?

I have a garage roller door that is triggered via a momentary switch (switch.sonoff_shed_door_relay_2). There is no way for the switch to know whether the garage door is open or closed as it goes on for 1 second then back to off.

I have a zigbee door sensor attached to the garage door that tells me whether it is open or closed (binary_sensor.shed_door_sensor_contact).

What I would like is to have the icon colour of the button I have assigned to trigger the shed switch get it’s state colour from the state of the door sensor. eg, it’s yellow when open, blue when closed.

Screenshot from 2022-03-26 17-09-47

You could do this with a switch template?

Thanks that looks like it will work.

Now to try to understand the instructions on that page and why I’m getting errors…
: Invalid config for [switch.template]: invalid slug Garage (try garage) for dictionary value @ data[‘switches’].

Can you share your config?

switch:
  - platform: mqtt
    unique_id: kyeoffice_fanlight
    name: "Kyes Office Fan Light"
    command_topic: cmnd/sonoff433/Backlog 
    optimistic: true
    payload_on: "RfRaw AA B0 29 03 08 0140 0366 0CC6 281818190909090819090818181818181908190908190818181819090819081818 55; RfRaw 0" 
    payload_off: "RfRaw AA B0 29 03 08 0140 0366 0CC6 281818190909090819090818181818181908190908190818181819090819081818 55; RfRaw 0" 
    retain: true

  - platform: mqtt
    unique_id: kyeoffice_fan
    name: "Kyes Office Fan"
    command_topic: cmnd/sonoff433/Backlog 
    optimistic: true
    payload_on: "RfRaw AA B0 29 03 08 0136 037A 0CDA 281818190909090819090818181818181908190908190909081819090819090908 55; RfRaw 0" 
    payload_off: "RfRaw AA B0 29 03 08 0136 037A 0CDA 281818190909090819090818181818181908190908190909081819090819090908 55; RfRaw 0" 
    retain: true

  - platform: template
    switches:
      garage:
        value_template: "{{ is_state('binary_sensor.shed_door_sensor_contact', 'open') }}"
        unique_id: shed_door_switch
        friendly_name: "Shed Door"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.sonoff_shed_door_relay_2

Not sure why, but this fixed the random error I was getting, but still not getting state from the door sensor to show as state for the templated switch.

  - platform: template
    switches:
      garage:
        value_template: "{{ is_state_attr('switch.sonoff_shed_door_relay_2', 'binary_sensor.shed_door_sensor_contact', 'on') }}"
        unique_id: shed_door_switch
        friendly_name: "Shed Door"
        turn_on:
          service: switch.turn_on
          target:
            entity_id: switch.sonoff_shed_door_relay_2
        turn_off:
          service: switch.turn_off
          target:
            entity_id: switch.sonoff_shed_door_relay_2

What you are describing is a cover, a term which includes doors, gates, windows, blinds and so forth.

Set up a template cover and you can use whatever sensors you like to tell the integration whether the door is open, closed, opening, etc.

This definitely looks close to what I want, still for the life of me can’t work out what I am doing wrong though. I’ve got the cover template actuating the shed door, but still not showing the correct state.

cover:
  - platform: template
    covers:
      shed_door:
        device_class: garage
        friendly_name: "Shed Door"
        position_template: "{{ states('binary_sensor.shed_door_sensor_contact') }}"
        open_cover:
          service: switch.toggle
          target:
            entity_id: switch.sonoff_shed_door_relay_2
        close_cover:
          service: switch.toggle
          target:

Screenshot from 2022-03-28 10-47-15

In the developer tools|states panel, tell us that the state of binary_sensor.shed_door_sensor_contact is when the door is open and closed.

position_template takes a value between 0 (closed) and 100 (open). Your binary sensor will be false or true I think. You may need to convert it to an int, and multiply by 100. But I am guessing until I know the states.

When open:
contact: false

When closed:
contact: true

I had also tried value_template as well, which I thought would use the true / false, but I got no change in behaviour.

The state is shown as on in your screenshot. Then you say when closed the state is true and when open it is false.

Which is it?

Sorry yes state is on. I converted it to the true / false as that is how it ends up being reported.

availability:
  - topic: zigbee2mqtt/bridge/state
device:
  identifiers:
    - zigbee2mqtt_0x00124b00226bc84c
  manufacturer: SONOFF
  model: Contact sensor (SNZB-04)
  name: Shed Door Sensor
device_class: door
json_attributes_topic: zigbee2mqtt/Shed Door Sensor
name: Shed Door Sensor contact
payload_off: true
payload_on: false
state_topic: zigbee2mqtt/Shed Door Sensor
unique_id: 0x00124b00226bc84c_contact_zigbee2mqtt
value_template: '{{ value_json.contact }}'
platform: mqtt

I think you’ll probably want your position_template to be something like

{{ iif(is_state('binary_sensor.shed_door_sensor_contact', 'on'), 100, 0) }}

Thanks Nickrout,

This worked. For anyone else:

cover:
  - platform: template
    covers:
      shed_door:
        device_class: garage
        friendly_name: "Shed Door"
        position_template: "{{ iif(is_state('binary_sensor.shed_door_sensor_contact', 'on'), 100, 0) }}"
        open_cover:
          service: switch.toggle
          target:
            entity_id: switch.sonoff_shed_door_relay_2
        close_cover:
          service: switch.toggle
          target:
            entity_id: switch.sonoff_shed_door_relay_2
        

2 Likes

Great - I was looking for this very solution.
Is the position reflected somewhow (if between 0 and 100?).
Thank you.

You have 2 solutions

  1. A sensor to get the amount of openness (eg a tilt sensor for a tilt door; or

  2. A time based solution, so if you know your door takes 10s to open, it is at 20% after 2s.

All dealt with here

Thank you - I will check this out.

The main problem for me is, that with this kind of cover Alexa is asking for a code to open the garage door and there is no option to decativate the code request.
This makes it unusable for me and I need to figure out another approach.
:cry: