Weird automation bug


Found a weird bug that I can not resolve. This automation gets triggered when my TV reconnects to MQTT… the following is visible on the console of the TV sonoff

08:40:13 MQT: Attempting connection...
08:40:13 MQT: Connected
08:40:13 MQT: tele/sonoff-dn-tv/LWT = Online (retained)
08:40:13 MQT: cmnd/sonoff-dn-tv/POWER = 
08:42:14 MQT: tele/sonoff-dn-tv/STATE = {"Time":"2018-12-27T08:42:14","Uptime":"2T08:16:30","Vcc":3.442,"SleepMode":"Dynamic","Sleep":50,"LoadAvg":19,"POWER":"OFF","Wifi":{"AP":1,"SSId":"hassio","BSSId":"00:25:9C:3D:1D:B2","Channel":3,"RSSI":60}}
08:44:29 DNS: Initialized

The automation itself that gets triggered right after that

- id: tv_off_hall_light_on_for_5_min
  alias: "Hall light on for 5 min if TV turned off after dark"
    - platform: state
      to: 'off'
    condition: and
      - condition: state
        entity_id: sun.sun
        state: 'below_horizon'
      - condition: state
        entity_id: switch.lamp
        state: 'off'
    - service: homeassistant.turn_on
      entity_id: switch.lamp
    - delay: 00:05:00
    - service: homeassistant.turn_off
      entity_id: switch.lamp

Add from: 'on' In your trigger.

When MQTT connects it pushes the latest value and updates the state of the sensor. Even if it was off before, the automation is triggered because of a state change (“off” to “off”). Adding the “from” makes sure this only triggers when it goes from “on” to “off”.

Perfect! Thank you!