Binary Sensor Dilemma

Sitting here in my workshop pulling my hair out… so after 5 hours I am asking for help.

Here’s the entry in my binary_sensor.yaml file…

# Reed Switch Sensor
 - platform: rpi_gpio
   invert_logic: false
   ports:
     24: Studio Door

Here’s the customisation I made…

The door status is showing in my overview, and when I open and close the door it changes status…

DoorStatus

Here is the entries from my automation.yaml file

mode: single
- id: '1615112667025'
  alias: Studio Door Closed Alert
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.studio_door
    from: 'On'
    to: 'Off'
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: HomeAssistant/Studio/Door/Status
      payload: '1'
  mode: single
- id: '1615113159409'
  alias: Studio Door Open Alert
  description: ''
  trigger:
  - platform: state
    entity_id: binary_sensor.studio_door
    to: 'On'
    from: 'Off'
  condition: []
  action:
  - service: mqtt.publish
    data:
      topic: HomeAssistant/Studio/Door/Status
      payload: '0'

So… my issue… the automation never triggers?? I am not sure if I should be using the combination of Open/Closed or On/Off to trigger the sending of the MQTT message?

Yes, I have tried copious amounts of combinations of both and am not getting anywhere. Is there somewhere I can see what I should be using to trigger the automation? The logs are useless, unless I am missing something?

I just want to send an MQTT message when the door is opened, and another when it is closed. All help greatly appreciated.

go into dev tools and check what the actual states are when open and close, then use those
Open your Home Assistant instance and show your state developer tools.

Hi There… thanks for dropping in. I’m assuming you mean this?

I also found this… which has been confusing?

Ignore @samnewman86 - he/she is talking nonsense.

All binary_sensor have the states ‘on’ and ‘off’ regardless of how they are represented in the UI (which is defined by the device class)

The problem with your automation is that you have capitalised the states when they need to be lowercase.

Thanks for the info… So, if I understand… I would use on and off in the automation? What would I use as the trigger, would that be ‘state’ or something else?

Yeah, state trigger, but with lowercase on and off.

Sorry for the dumb questions… I’ve set the state as follows…

To trigger the door closed, I trigger on the state going from on to off
And to trigger the door open, I trigger on the state going from off to on

I figure that should be right?

Certainly sounds right, unless your binary sensor’s states are the reverse of ‘normal’.

If you wanted to be a yaml ninja this could all be done in one, short automation…

- alias: Studio Door Status Alert
  trigger:
    platform: state
    entity_id: binary_sensor.studio_door
    to:
      - 'on'
      - 'off'
  action:
    service: mqtt.publish
    data:
      topic: HomeAssistant/Studio/Door/Status
      payload: "{{ '1' if trigger.to_state.state == 'off' else '0' }}"

Thank you so much, just tested it and works awesome. Really appreciate the help :grinning:

I’ll try your suggestion as well… but I wont consider myself a yaml ninja just yet.

1 Like

Attempted your automation yaml, but it gave me an error… Message malformed: expected dictionary

No idea what that means?

Sorry that’s where I thought you were looking and saw Closed?

Nor me tbh, but it should be a valid automation :man_shrugging: - not to worry, just stick with using the two that are working :slightly_smiling_face: