Turn Off Binary Sensor using trigger entity_id

Hi,

I’m going round in circles with what I thought would be a super simple automation;

- id: Reset phone interactive
  alias: reset phone interactive
  trigger:
    - platform: state
      entity_id:
        - binary_sensor.sm_g970u_interactive
        - binary_sensor.sm_s901b_interactive
      to: 'on'
      for: '00:01:00'
  action:
    - service: homeassistant.turn_off
      entity_id: "{{ trigger.entity_id }}"

Gives:

Invalid config for [automation]: not a valid value for dictionary value @ data['action'][0]['entity_id']. Got None. 

If I replace the actions with:

  action:
    - service: homeassistant.turn_off
      entity_id: group.phone_display_on

I don’t get an error. The automation triggers ok, but it doesn’t turn off the sensors.

Group config is:

phone_display_on:
  name: Phone Display On
  entities:
    - binary_sensor.sm_g970u_interactive
    - binary_sensor.sm_s901b_interactive

The reason for all this, is that the interactive sensor in the companion app sticks at ‘on’ when I power off my phone at night, which stops other stuff triggering in the morning.

Where am I going wrong???

Thank you

:slight_smile:

A binary_sensor (as far as I know) cannot be updated using Home Assistant services. This binary_sensor reflects the state of a device but you cannot change the state of the device by changing the binary_sensor…

Look at this post for a possible solution to “force” a sensor/binary_sensor to change its value:

1 Like

Hmmm, thanks. What a hassle that looks like, cos I can set the binary sensor state in the UI.

Just thinking… maybe I should make a dummy input boolean and a trigger that updates it according to the binary sensor. Then I could at least switch the input boolean on and off as I need to.

An input_boolean would be a better solution… than “tweaking” a binary_sensor…

1 Like

What if you use the update_entity service as automation action?

Maybe take a step back. Why do you want a binary sensor to go off one minute after it goes on? What does the binary sensor represent? Maybe you could have a template binary sensor that goes on when a binary sensor goes on, then goes off one minute later.

1 Like

Whole reason for this is that I have an automation that is triggered in a morning when the ‘interactive’ binary sensor on the companion android is turned on.
The interactive sensor directly relates to whether the phone screen is on or off. In a morning, I might look at my phone as soon as I wake up, if I do (or one of other other unrelated triggers fire), the automation starts a ‘waking up’ script that sorts other bits and bobs out.

Problem is… that if I decide to power off my phone when I go to bed, the interactive sensor never updates to say the screen is off - presumably because the app is terminated before it can turn off the sensor. So the sensor stays on all night, even though the phone screen is off (because I powered it off).

So I’m trying to get an automation to set the binary sensor to off, if it gets stuck at ‘on’ for a period of time. I set it to 1min just to test it, but in reality, I’ll set it to an hour.

Ok, I don’t use the companion app, so can’t help with that, but yeah, it sounds like an intermediary input_boolean can help here. I actually have a similar scenario with a Z-Wave multisensor. It’s motion sensor is represented by two sensor entities, and sometimes one of them gets “stuck” on for some reason. I’ve created an input_boolean that is turned on and off via an automation. It’s probably more than you need, but here’s what I have:

input_boolean:
  hf_motion:

automation:

# binary_sensor.house_front_sensor and sensor.house_front_burglar will both
# generally change when motion is detected and no longer detected. However, not
# always. Sometimes only one changes to "on". Also, sometimes one of them will
# not go back "off" (until the next motion event.)
#
# To make sure we get one trigger event per motion event we can't simply "or"
# them. So instead we'll turn an input_boolean on whenever either indicates
# motion, and then back off whenever either of them stops indicating motion.
# We'll also turn off the input_boolean if either of the sensors gets "stuck"
# in the on state for too long.

  - alias: HF Motion - On
    mode: restart
    trigger:
      - platform: state
        entity_id: binary_sensor.house_front_sensor
        to: 'on'
      - platform: numeric_state
        entity_id: sensor.house_front_burglar
        above: 0
    action:
      - service: input_boolean.turn_on
        entity_id: input_boolean.hf_motion

  - alias: HF Motion - Off
    mode: restart
    trigger:
      - platform: state
        entity_id: binary_sensor.house_front_sensor
        to: 'off'
      - platform: numeric_state
        entity_id: sensor.house_front_burglar
        below: 1
      - platform: state
        entity_id: binary_sensor.house_front_sensor
        to: 'on'
        for:
          minutes: 30
      - platform: numeric_state
        entity_id: sensor.house_front_burglar
        above: 0
        for:
          minutes: 30
    action:
      - service: input_boolean.turn_off
        entity_id: input_boolean.hf_motion

  - alias: HF Motion - Stuck Sensor
    trigger:
      - platform: state
        entity_id: binary_sensor.house_front_sensor
        to: 'on'
        for:
          minutes: 15
    action:
      - service: zwave.refresh_entity
        # Must use data:, otherwise entity_id will be turned into a list,
        # and zwave.refresh_entity only accepts a single entity_id.
        data:
          entity_id: binary_sensor.house_front_sensor
      - service: persistent_notification.create
        data:
          message: House Front Sensor was stuck!

  - alias: HF Motion - Stuck Burglar
    trigger:
      - platform: numeric_state
        entity_id: sensor.house_front_burglar
        above: 0
        for:
          minutes: 15
    action:
      - service: zwave.refresh_entity
        # Must use data:, otherwise entity_id will be turned into a list,
        # and zwave.refresh_entity only accepts a single entity_id.
        data:
          entity_id: sensor.house_front_burglar
      - service: persistent_notification.create
        data:
          message: House Front Burglar was stuck!

Yeah, was thinking same.

But I just realised I could prob do what I need with a simple template sensor

template:
  - trigger:
      - platform: state
        entity_id: group.phone_display_on
    binary_sensor:
      - name: phone_display_trigger_sensor
        auto_off: '01:00:00'
        state: >
          {% if is_state('group.phone_display_on', 'on') %}
            on
          {% else %}
            off
          {% endif %}


Just tested OK. It switches the trigger sensor off if it stays on for 1 hour. Perfect for me.

:slight_smile:

Yep, I kind of thought of that, but I wrote my code years ago and haven’t bothered to update it. Also, I’m stuck on an old version of HA for some lame reasons. lol

Why not just:

state: "{{ is_state('group.phone_display_on', 'on') }}"

Or:

state: "{{ states('group.phone_display_on') }}"

Or even:

state: "{{ trigger.to_state.state }}"

ooooo, nice - thats even better.

All sorted, thank you :slight_smile: