Temperature-based switch automation not firing consistently

I’m pretty new to HA, but I’m savvy enough to steal other people’s configs and make stuff happen in my setup.
I got a Xiaomi hub with a temperature sensor, and managed to get a switch to turn my heater on and off, when the temperature dips below a given temperature and off again when it goes above a given temperature. It was also time-restricted but I’ve just taken that out in case it was the issue…

What is the issue? Well, the symptom is that the automation rarely fires of its own accord. Right now for example the temperature is below the threshold (and the time is past the now-removed time restriction) but HA is just staring blankly at me, and not firing the automation. I can trigger it manually, so I know that it does work. And it will fire itself, now and again (although more of the again than the now).
The entry from my config is:

 - alias: "Turn on Heater"
   hide_entity: False
   trigger:
     platform: numeric_state
     entity_id: sensor.temperature_158d0001b8cb5b
     below: 16
   condition:
     - condition: time
       after: '18:00:00'
       #before: '22:30:00'
   action:
     - service: homeassistant.turn_on
       entity_id: switch.plug_158d00023ee1b8
     - service: light.turn_on
       data:
        entity_id: light.gateway_light_7811dce1774a
        brightness: 255
        color_name: red
       
automation heater off:
 - alias: "Turn off Heater"
   hide_entity: False
   trigger:
     platform: numeric_state
     entity_id: sensor.temperature_158d0001b8cb5b
     above: 18
   condition:
     - condition: time
       after: '18:00:00'
       #before: '22:30:00'
   action:
     - service: homeassistant.turn_off
       entity_id:
        - switch.plug_158d00023ee1b8
     - service: light.turn_on
       data:
        entity_id: light.gateway_light_7811dce1774a
        brightness: 255
        color_name: blue```

It will only trigger if the temperature goes from 16 or above to below 16 after 18:00.

If the temperature is already below 16 when 18:00 rolls around, the automation won’t trigger. You need to account for that in your trigger and conditions:

 - alias: "Turn on Heater"
   hide_entity: False
   trigger:
     - platform: numeric_state
       entity_id: sensor.temperature_158d0001b8cb5b
       below: 16
     - platform: time
       at: '18:00:01'
   condition:
     - condition: time
       after: '18:00:00'
     - condition: numeric_state
       entity_id: sensor.temperature_158d0001b8cb5b
       below: 16

That makes total sense and fits the symptoms!

I’ll try that and let you know how I get on…

Hmmm, II have updated my config using your code, and changed the time to trigger now (well, 15 minutes ago), and …nada… So, back to the drawing board…

It’ll trigger at that time. If it’s already after 18:00:01 (or whatever time) then the automation won’t trigger. I assume you’d changed the automation and reloaded it suitably in advance?

Yes I changed and reloaded prior. :slight_smile:

It just triggered. I assume that a change in the sensor’s temperature is what actually triggers the event, when all the other criteria have been met? In other words, if the temperature stays the same, HA is not alerted by the sensor as nothing has changed…?

But as I say, it just triggered - looking at the history the temp just dropped a touch and I’m guessing that is what triggered the automation.

Yes. For the above to trigger one of the following has to happen:

  • The time must be 18:00:01
  • The temperature goes from 16 or higher to below 16

If the temperature never changes then it relies upon the time trigger to fire.

Awesome, thanks. I’ll change the time to correct range and see what happens tonight.