Αssociate a binary sensor state with a switchbot for a cover template

Hi all!

My problem is simple, but I can’t find any documentation about it:

I have a binary door sensor attached to a door which can be opened through a switch (switchbot), or manually.
The correct state of the door (on/off) is on binary door sensor, not on the switch.

So I need a way to change the state of a switch without execute the command, associating the switch state (on/off) according to the door sensor.

Any ideas?

If I understand you correctly this can be done with an automation. Trigger on the binary sensor (any state), set the switchbot switch with an action template dependant on the ‘trigger to state’. If you list your switch and sensor entity_ids I can give you an example.

The entities are:

  • switch.switchbot
  • binary_sensor.door_window_sensor_158d00026edec3

I thought if I create an automation with a trigger, the action will execute also the switch (not only the state).

Send me your example and I will test it though :slight_smile:

Thanks!

Yes it will. That’s what you asked for:

I just rephrase what I asked in order to be more clear!

I have a NEO Door/window sensor (named binary_sensor.027_closet_lr_sensor attached to a closet door and a light inside the closet (named light.l_lr_closet) that is supposed to turn on/off when the door is open/closed - guess that’s pretty much the same as you’re trying to make.
The code is:

  - alias: "L-Closet Living Room: Door open"
    trigger:
      - platform: state
        entity_id: binary_sensor.027_closet_lr_sensor
        to: 'on'
    action:
      - service: light.turn_on
        entity_id: light.l_lr_closet

  - alias: 'L-Closet Living Room: Door closed'
    trigger:
      - platform: state
        entity_id: binary_sensor.027_closet_lr_sensor
        to: 'off'
    action:
      - service: light.turn_off
        entity_id: light.l_lr_closet

Thanks for your reply, but this isn’t what I want.

The action I need is to change the state without turning on or off the switch sensor

Anybody?

I think the confusion might be the fact that you are trying to “fake out” the state of the switch based on the current state of the door sensor.

If I can ask…What is the true state of the switch when the door is open? Obviously the state of the switch doesn’t already follow the state of the door or you wouldn’t be asking.

Wouldn’t it make more sense to show the true state of the switch (on/off) but then just display the state of the door separately?

If you really need to show the switch state to follow the state of the door sensor the closest I can think would be a template switch. But even then the command_on & command_off are required (I think) so even then if you click on the switch then it will still act strangely compared to a “real” switch.

Imagine the door can be opened by anyone (it is a garage door). I use a switchbot in order to press a controller to open. After 1 minute the door closes automatically, but the switchbot’s state is still on.
I want to use a binary sensor in order to get the correct state (opened/closed) and set it to the switchbot state.

The solution was supposed to solve my case is to change the state through REST API, but because the switchbot is developed as a “Local Polling” ( https://www.home-assistant.io/blog/2016/02/12/classifying-the-internet-of-things/#classifiers ), when I change the state, after a few seconds is changing back.

That means it’s talking to the device, and getting the state from there, but the switchbot does not have any state stored. I think I need to close this case and open to developers another…

I will keep it for a couple of days if someone knows the solution in here.

Ok, now we are getting somewhere. :smiley:

You have a garage door opener that uses either a remotely operated switch or a locally operated operator of some kind (manual, whatever).

Does the switch require to be kept on when the door is open? IOW, if the switch is on (because you previously used it open the door) and then someone manually closes the door and then you turn off the switch does the door do anything at that point? Does it stay closed or will it open back up?

The reason for the questions is that I also have a garage door opener that uses both a remotely controlled switch thru HA and also RF remote control that came standard with the door. HA can know that the door is opened by another door sensor in a similar way to the way yours works.

The door switch controlled by HA only has to be a momentarily operated switch (like a regular pushbutton). So the way I handle that is that when I activate the switch from HA to open the door then I have an automation that turns the switch back off in one second.

I really don’t care, per se, what the state of the switch is I only care if the door is open or closed. But I do “kind of” care what the state of the switch is but only for completeness sake if you understand me. If the switch state shows “off” but it is in reality “on” then I can see things getting confusing.

Maybe your situation is the same as mine and this might help you think of it in a different way.

Or I’m completely off base and would need additional information to help more. :wink:

Either way, I’m good.

Ok, You are good :stuck_out_tongue:
Yes if my switch is on and someone closes the door (actually it closes after 1 min or so automatically), the switch is remaining on.
The work around with the automation it could be ok, IF you can turn the switch back off without pressing it. If you really press the button when the door is closing it will stop and it will remain semi-open which is the worst case scenario…

To minimize confusion I’m going to call the actual normal garage door operator pushbutton the “GDO” and the HA controlled button the “switchbot”

Looking at the switchbots I think they only act as kind of a “virtual finger” pressing the GDO button just like you would when you operate the GDO.

So (and correct me if I’m wrong…) when you normally push the GDO you only momentarily push it then immediately release it. If you turn on the switchbot then turn it back off right away doesn’t it just “unpress” the GDO just like you pulling your finger away from the GDO?

Or does your GDO need to be held in position until the door goes all the way closed or open?

I know the way my GDO works is if I push the button momentarily the door operates (open or closed). But if it’s opening and is half way open and I push the GDO again it stops opening and stays in that position. And if it’s going closed and I push the GDO again it stops and goes back open.

I think you guys are looking at this the wrong way. This is a cover, not a switch. Template cover may be what you want.

I agree that is eventually the goal but I’m just trying to work out how the switching works before the template can even be figured out.

It suits to my case!
The problem I have now is that the valid values for value_template are: open / true or closed / false and the binary sensor returns on/off, should I give a try with these values or there is a way to transform them to open/closed ?
One of the examples has the following, should I try it?
value_template: "{{ states('sensor.garage_door')|float > 0 }}"

You can do what ever you want pretty much. That’s why you use a template.

But you can use the ‘on’ value. Here is mine:

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

I’m not sure how this fixes your original problem. Won’t your switchbot still be in the wrong state if someone operates the door with the GDO?

I will get the state from the binary sensor, not from the switchbot :wink:

Oh, I thought you wanted the switchbot to take on the state of the door since you already had the state of the door from a door position sensor.

I guess I’m still confused. :crazy_face:

But I’m glad you got it worked out.

So, the default template works perfectly!

  - platform: template
    covers:
      outside_door:
        friendly_name: "Outside Door"
        value_template: "{{ states('binary_sensor.door_window_sensor_158d00026edec3')|regex_replace('on','open')|regex_replace('off','closed') }}"
        open_cover:
          service: switch.turn_on
          data:
            entity_id: switch.switchbot
        close_cover:
          service: switch.turn_off
          data:
            entity_id: switch.switchbot
        stop_cover:
          service: switch.turn_on
          data:
            entity_id: switch.switchbot
        icon_template: >-
          {% if is_state("binary_sensor.door_window_sensor_158d00026edec3", "on") %}
            mdi:garage-open
          {% else %}
            mdi:garage
          {% endif %}```
2 Likes

Can this be used to ask google the state of a sensor?