Toggle binary sensor to use as dog presence detection

Hi, i need some help with the following:

I have a door sensor installed on my dogs door hatch to go to the backyard. I want to use that to toggle a binary sensor that i can use to tell if my dog has gone through the hatch and is on the backyard or if it has returned. Ideally as a switch as well so i can re-sync the status as needed. Any ideas suggestions to do this? can i toggle a switch template everytime the hatch sensor moves from on to off?

thanks in advance

Use an input_boolean if you wan to adjust it occasionally yourself.

input_boolean:
  dog_in_yard:
    name: Dog In Yard
    icon: mdi:dog
automation:
  trigger:
    platform: state
    entity_id: binary_sensor.dog_door
    to: 'on'
  action:
    service: input_boolean.toggle
    entity_id: input_boolean.dog_in_yard

thank you. i am having a problem though. I can see the “binary_sensor.dog_hatch” state moving from off to on in the states page. But for some reason it doesnt trigger the automation. Here is the full code.

binary_sensor:
  - platform: mqtt
    name: "Dog hatch"
    state_topic: "rtl_433/Generic-Remote/23868/cmd"
    payload_off: "14"
    payload_on: "10"
    device_class: opening

input_boolean:
  dog_in_yard:
    name: Dog In Yard
    icon: mdi:dog

... in automations.yaml file...

- alias: Dog status
  trigger:
    platform: state
    entity_id: binary_sensor.dog_hatch
    to: "on"

  action:
    service: input_boolean.toggle
    entity_id: input_boolean.dog_in_yard

binary_sensor.dog_hatch works well on its own as i can monitor it in the states page from off to on. I can also manually trigger the automation from the automations menu and it toggles the input_boolean successfully. So it looks like the “on” state change is never recognized by the trigger

Do you think that the device_class has anything to do with it?

No, binary sensors always have a back end state of on or off no matter what is displayed via the device class in the front end.

Are you sure your automation is on (enabled)?

Adding initial_state: true will ensue it always is:

- alias: Dog status
  initial_state: true
  trigger:
    platform: state

thanks you, that was it. didnt remember that one. I added initial state to true as well. it works great now

1 Like