Automation with binary sensor fails

Hi All!

I’ve just installed HA (python venv v0.60.1) and am having issues with this automation that sometimes does not trigger.

Yesterday evening exterior lights were turned on ok but this morning they were still on after sunrise. I checked the state of the binary sensor and it is correctly set. If I manually trigger the binary sensor the automation works.

I find nothing in the error log that suggest any issues.

automation.yaml:

- alias: "Exterior Lights"
  trigger:
    - platform: state
      entity_id: binary_sensor.daylight
    - platform: homeassistant
      event: start 
  action:
    - service_template: >
        {% if is_state('binary_sensor.daylight', 'on') %}
          light.turn_off
        {% else %}
          light.turn_on
        {% endif %}
      entity_id: light.exterior

configuration.yaml:

binary_sensor:
  - platform: template
    sensors:
      daylight:
        friendly_name: "Daylight"
        value_template: >-
          {{ states.sun.sun.attributes.elevation | float > -4.0 }}

light:
  - platform: mqtt
    name: Exterior
    state_topic: "node/esp001/relay"
    command_topic: "node/esp001/relay/set"
    payload_off: 0
    payload_on: 1
    qos: 1

Your trigger looks funky to me,

I’ll assume that it defaults to looking for a transition to the ‘on’ state if it isn’t explicitly defined, since you say that the lights do come on at ~sunset.

but then there is nothing to cause the automation to trigger again when the state of the binary sensor goes to ‘off’.

My way might not be the most elegant but i would split it into two automations: one for the on & then another for the off and I would explicitly define the state of the trigger to cause the action. "to: ‘on’ " and "to: ‘off’ ".

but then someone may come along and prove that completely unnecessary. but it will work! :smiley:

And i’m not sure if you realize it but you also have a second trigger that will cause the lights to come on or go off (depending on the current state of the binary sensor) when HA starts?

There is, a state trigger fires whenever the state or attributes change if you don’t specify the to or from states.

Did you check the Logbook to see if the automation fired?

Just wondering if the automation fired on schedule, but something MQTT related didn’t play ball.

I didn’t know that. (obviously :smiley: )

Thanks for the info!

Thanks finity.

The automation fails equally on “on” or “off” state changes. Some evenings the light does not go on and some mornings it does not go off.

The docs (https://home-assistant.io/docs/automation/trigger/) say that a state trigger “Triggers when the state of tracked entities change. If only entity_id given will match all state changes, even if only state attributes change.” so there should be no need to specify the state changes.

Could someone confirm that state change triggers are finicky and better entered as separate events?

For readability it’s recommended to do a separate automations for the different outcomes, but there’s nothing wrong with what you’ve done.

It should fire just fine, which is where the Logbook will come in. We’ll know if the automation isn’t firing (trigger problem) or is, but the action isn’t completing (MQTT problem).

Thanks Tinkerer,

You are right, the loogbook entry (in reverse chronological order):

6:26 AM Exterior Lights has been triggered
6:26 AM Daylight turned on

So I take that the issue is with my mqtt connection.

The entity info on lights.exterior showed on (when they should have been off).

I’m new to mqtt so would like to know if is there is a way to keep publishing the light.off/on until the node confirms a switch?

I don’t use MQTT I’m afraid, but, you could do something like this. That’ll wait 30 seconds then check the state of the lights. If they’re not in the relevant state it’ll re-issue the command.

You should be able to put that into a loop.

- alias: "Exterior Lights"
  trigger:
    - platform: state
      entity_id: binary_sensor.daylight
    - platform: homeassistant
      event: start 
  action:
    - service_template: >
        {% if is_state('binary_sensor.daylight', 'on') %}
          light.turn_off
        {% else %}
          light.turn_on
        {% endif %}
      entity_id: light.exterior
    - delay: '00:30:00'
    - condition: template
      value_template: >-
        {% if is_state('binary_sensor.daylight', 'on') %}
          {{ is_state('light.exterior', 'off' }}
        {% else %}
          {{ is_state('light.exterior', 'on' }}
        {% endif %}
    - service_template: >
        {% if is_state('binary_sensor.daylight', 'on') %}
          light.turn_off
        {% else %}
          light.turn_on
        {% endif %}
      entity_id: light.exterior

Esp8266 is configured like:

  // Switch off the relay if 1 was received as first character. Relay connected NC. HIGH/LOW are reversed.
  if ((char)payload[0] == '1') {
    digitalWrite(RELAY_PIN, LOW);
    client.publish("node/"CLIENT_ID"/relay", "1", true);
  } else {
    digitalWrite(RELAY_PIN, HIGH);
    client.publish("node/"CLIENT_ID"/relay", "0", true);
  }

Great Tinkerer!

I will add this and report back.

Ok, I am resorting to dual automations to test mqtt. Also installed mosquitto and am using that instead of embedded. I’ll keep testing for a few days…

- alias: "Exterior Lights On"
  trigger:
    - platform: state
      to: 'off'
      entity_id: binary_sensor.daylight
    - platform: homeassistant
      event: start
  condition:
    condition: state
    entity_id: binary_sensor.daylight
    state: 'off'
  action:
      service: light.turn_on
      entity_id: light.exterior

- alias: "Exterior Lights Off"
  trigger:
    - platform: state
      to: 'on'
      entity_id: binary_sensor.daylight
    - platform: homeassistant
      event: start
  condition:
    condition: state
    entity_id: binary_sensor.daylight
    state: 'on'
  action:
      service: light.turn_off
      entity_id: light.exterior