Heating automation doesn't start

Hello everyone and thanks for your help. I’m trying to automate the heating of my room.

I have 2 automations to maintain the temperature of the room between 19ºC and 19.5ºC between 6pm and 7am. The automation #1 turns on a radiator (through a TP-Link smart plug) when the temperature is below 19ºC (measured by a xiaomi-mijia BLE temperature sensor: LYSWSD03MMC) between 6pm and 7am. The automation #2 turns off the radiator when the temperature is above 19.5ºC.

My code:

- id: '1609100087847'
  alias: 'Heating on'
  description: ''
  trigger:
  - type: temperature
    platform: device
    entity_id: sensor.ble_temperature_termometro
    domain: sensor
    below: 19
    above: 0
  condition:
  - condition: time
    after: '18:00:00'
    before: '7:00:00'
  action:
  - type: turn_on
    entity_id: switch.enchufe
    domain: switch
  mode: single
- id: '1609100239043'
  alias: 'Heating off'
  description: ''
  trigger:
  - type: temperature
    platform: device
    entity_id: sensor.ble_temperature_termometro
    domain: sensor
    above: 19.5
    below: 50
  - platform: time
    at: '6:59:00'
#I had to set up the time as a trigger to turn off the plug at 6.59 because sometimes the plug didn't disconnect and continues working all day.
  condition:
  - condition: time
    after: '18:00:00'
    before: '7:00:00'
  action:
  - type: turn_off
    entity_id: switch.enchufe
    domain: switch
  mode: single

The problem is that the automation doesn’t start automatically when the temperature of the room is below 19ºC at 6pm. I have to manually turn on and turn off the plug and, then, the automation begins to work properly. It seems like if I had to “refresh” the automation through the manual activation/deactivation of the plug.

Thanks.

Shouldn’t the times be expressed with leading zeros, as in 07:00:00 versus 7:00:00? Other than the possibility that your sensor measurements are coming is as strings, it seems OK.

Here’s what’s happening (I think - let me know if this isn’t the case for you):

  1. Temperature drops to below 19C sometime before 1800
  2. That triggers the automation
  3. The condition blocks it because it’s not after 1800
  4. It won’t trigger again until the temp rises to above 19C and then drops again to below 19C

You’re using a temp sensor and switch, so that in essence is a thermostat. So… eliminate the condition and replace your action section with this:

  action:
  - service: >
      {% set current_time = as_timestamp(now())|float %}
      {% set target_time_start  = as_timestamp(now().replace(hour=18).replace(minute=00).replace(second=0).replace(microsecond=0))|float %}
      {% set target_time_end    = as_timestamp(now().replace(hour=7).replace(minute=00).replace(second=0).replace(microsecond=0))|float  + 24.0*3600.0 %}
      {% if states('sensor.ble_temperature_termometro')|float < 19.0 and 
            current_time > target_time_start and 
            current_time < target_time_end %}  switch.turn_on
      {% else %}                               switch.turn_off
      {% endif %}
    entity_id: switch.enchufe

This calculates the current time, the target start time (1800) and then the end time (add 24 hours worth of seconds to today at 0700 to get the timestamp for tomorrow at 0700. Now that I’m looking at this, the start and end times only work correctly until midnight. After midnight, the start time will be 1800 later that day (tomorrow), which will break the logic. We need to add a little more sophistication to the start and end times to cover the period between midnight and 0700. I need to spend time with the family right now, but if you need help adjusting the start and end times, I can do that tomorrow.

Hope this at least gets you started! Let me know if you need more help!

Many thanks @KSC for your assistance. Your hypothesis makes sense. I’m beginner with Home Assistant and I thought that the automation checked triggers and conditions recurrently. For the same reason, I have difficulties interpreting code (and for now I don’t have enough time to learn in depth). So, I was considering another “easier” solutions.

  • Is there any way that automations reload every X time to check if triggers and conditions meets? Thus, the timer condition didn’t block the trigger.
  • Does exist the possibility to create a new entity based on temperature? For instance, a new entity “room temp” (based on the temperature of the sensor) with several states: cold (below 19), normal (between 19-20) and hot (above 20). Thus, the plug triggers when the state is “cold”. Would it solve the problem?
  • The last possibility would be to create a third automation that triggers on/off the plug at 18.00, and later the automation runs in a normal way.

Thanks for your help. I think you guessed correctly, but I don’t feel reliable (and with enough time) to deal the problem through code.

Can definitely do this. Add another trigger to your current automation that uses the time_pattern platform. You can do a lot of things with that

  • trigger at a specific time every hour
  • trigger every x minutes
  • and a few more
    I think what you’re after is something like this:
  trigger:
    platform: time_pattern
    # You can also match on interval. This will match every 5 minutes
    minutes: "/5"

The other options are (as shown in the docs):

  trigger:
    platform: time_pattern
    # Matches every hour at 5 minutes past whole
    minutes: 5

automation 2:
  trigger:
    platform: time_pattern
    # Trigger once per minute during the hour of 3
    # The asterisk is a wild card that represents any minute of the hour. 
    # The hours limits it to only that hour
    hours: "3"
    minutes: "*"

If you add one of these in your current automation, then that can act as the trigger along with what you already have.

A little more explanation can be found in the docs at: Automation Trigger - Home Assistant (home-assistant.io)

I’m not sure this will give you what you want, though. Let’s say it’s 1730 and the automation triggers based on the time_pattern. It won’t get past the condition, which is good. Now, let’s say it’s 1805 and the time-pattern triggers. It’ll make it past the condition into the action, which will turn the switch off no matter what the temperature is. So I think you’ll still need a template in the service section, but it’s a lot less complicated because we’re no longer worried about the time. It would simplify to this:

  action:
  - service: >
      {% if states('sensor.ble_temperature_termometro')|float < 19.0 %}  switch.turn_on
      {% else %}                                                         switch.turn_off
      {% endif %}
    entity_id: switch.enchufe

This should be able to paste right into the automation. In fact, now you could get rid of the sensor trigger because it’s redundant. ANd you should be able to eliminate the second automation because it’s now covered with the modified first automation and the template above.

This is a little more complicated, but can be done with a template (e.g., programming). I have some “created” sensors where the state is something, and then the other things I want to track are attributes. If you’re interested in going that path, I can share a version of a template that should work. Let me know!

Thanks @KSC.

Your solution with the template in the service section works -almost- perfect! The code:

- id: '1609100087847'
  alias: 'Heating on'
  description: ''
  trigger:
  - platform: time_pattern
    minutes: /5
  condition:
  - condition: time
    after: '18:00:00'
    before: '7:00:00'
  action:
  - service: >
      {% if states('sensor.ble_temperature_termometro')|float < 19.0 %}  switch.turn_on
      {% else %}                                                         switch.turn_off
      {% endif %}
    entity_id: switch.enchufe

The problem is that last morning the heating was still functioning after 7.00. Any idea?

Finally, if you let me a last question: is there any way to introduce the template through the UI. I looked at the automation in the user interface and it looks like the next image, which I think is not fully correct. Thanks again for your help!

I’m guessing it’s this:

  1. Before 0700, the heating is on because of the temperature condition (a good thing)
  2. Then at 0700 and later, the condition keeps it from running the action section, which means if the heater was on, it’s going to stay on.

I missed this logical conundrum the first time around - interestingly, syntax errors are much easier to correct than holes in logic (and I’m sure I have several I haven’t found yet in my own code!).

The easiest way to make sure this doesn’t happen is to set up a second automation that simply turns the heater off at 0700. That would make sure it is off between 0700 and 1800. It would trigger at, say, 0701 to make sure it doesn’t get in a race with the original, and then the only action is to turn off the switch.

I’ll admit I hardly ever use the UI for editing any of the config files, so there might be a way that I’m not aware of. But after playing around with it for a few minutes, I don’t see how to do it. If you click the three vertical dots, there’s an option to edit as yaml. I did that, but it’s less than satisfying for anything longer than a line or so. It seems a little limited. Can you edit the yaml file directly? If so, that’s the best route to go (IMHO).

If you’re not sure how to do that, here’s a starting point: Advanced Configuration - Home Assistant (home-assistant.io). This page explains how to install the UI file editor that can be used to edit all the configuration files. Another option is to install the add-on for Samba share (which is what I use).

Glad this worked out well!

Thanks again @KSC for all your assistance. I marked the post as solved as your code works great! I agree that introducing the second automation is the easiest way to be sure that the heating will be off. So it is also ready!

1 Like

Glad it’s working!!