Template temperature sensor to switch heater with schedule

Hi
I’m new on Home Assistant and I tried by myself to learn how to make automation.
But now I have an issue that I can’t solve.
I have programme the shelly switch to fire on when the sensor temperature is below 19°C on a scheduled way.
But nothing happens when the condition is fulfilled.
Can you help ?

  alias: Rika Allumage
  description: ''
  trigger:
  - platform: template
    value_template: "{% if states('sensor.temperature_XXXXXXX') | float < 19 %} true {% endif %}"
  condition:
  - condition: time
    after: 06:00
    before: 07:30
    weekday:
    - mon
    - thu
    - fri
  - condition: or
    conditions:
    - condition: time
      after: 06:00
      before: '12:00'
      weekday:
      - tue
  - condition: or
    conditions:
    - condition: time
      after: 06:00
      before: '19:30'
      weekday:
      - wed
  - condition: or
    conditions:
    - condition: time
      after: 08:30
      before: '19:30'
      weekday:
      - sat
      - sun
  - condition: or
    conditions:
    - condition: time
      after: '16:00'
      before: '19:30'
      weekday:
      - mon
      - tue
      - thu
      - fri
  action:
  - repeat:
      sequence:
      - service: switch.turn_on
        target:
          entity_id: switch.shelly1_XXXXXX
      - delay: 00:00:02
      until:
      - condition: state
        entity_id: switch.shelly1_XXXXXX
        state: 'on'
  mode: single

The conditions are messed up. All conditions on top-level are implicit AND junction. Your current definitions results in an unmatchable time slot.

Also AND and OR conditions require more than one sub-condition (makes no sense otherwise). Create a top-level OR condition and make all of your current conditions it‘s children.

Finally, user numeric_state trigger instead of template trigger, especially since your current template trigger is lacking the „false“ branch.

Hi, thanks for your help, but my sensor is a Aqara temperature sensor, and as I have checked, the temperature is on a string value, not a numeric value, that’s why I have to set a template.

I am sure that it will do automatic conversion, but if you like you may use the example from documentation:

automation:
  trigger:
    - platform: numeric_state
      entity_id: sensor. temperature_XXXXXXX
      # Optional
      value_template: "{{ state | float }}"
      below: 19

Anyhow, the main issue is the conditions sequence. Again:

  • one OR condition at top-level
  • all time slots as child conditions

Going to try your suggestions, and give any news

  alias: Rika Allumage
  description: ''
  trigger:
  - platform: numeric_state
    entity_id: sensor.temperature_XXXXXXX
    below: 19
  condition:
  - condition: OR
    conditions:
    - condition: time
      after: 06:00
      before: 07:30
      weekday:
      - mon
      - thu
      - fri
    - condition: time
      after: 06:00
      before: '12:00'
      weekday:
      - tue
    - condition: time
      after: 06:00
      before: '19:30'
      weekday:
      - wed
    - condition: time
      after: 08:30
      before: '19:30'
      weekday:
      - sat
      - sun
    - condition: time
      after: '16:00'
      before: '19:30'
      weekday:
      - mon
      - tue
      - thu
      - fri
  action:
  - service: switch.turn_on
    entity_id: switch.shelly1_XXXXXX
  mode: single

Many thanks, I was checking my script while receiving your correction :+1:

So, I finally understand the Conditions function. That seems to be simplier and cleaner than what I have written. now I’m going to test that.

This

- condition: OR

Should be lower case:

- condition: or

that’s why I have an error notification and being looking for errors but no success !!
Many many thanks

All states are strings. Even if they look like a number.

The numeric_state trigger will convert the string into a number.

At this moment (08:56), the temperature is 15.9°C and the heater is still off. The shelly is off.
I don’t understand why the switch is still off.
Another question about the trigger “time”, The trigger is set to a exact time ( ie at: 08:00) do you think the automation will be triggering after 08:00, if I modify the programmation after the trigger time ? Do I have to launch the automation manually?

Did it fall below 19 / pass the threshold since Home Assistant started?

Why does everyone try to make a complex script or automation for a basic thermostat when there’s a perfectly good component.
Just create a generic thermostat, then schedule the setpoint changes at given times.

1 Like

Good idea. Wanna provide an example, please?

Yes, from Yesterday evening, 11PM the temperature is below 19,

In config.yaml

climate:
  - platform: generic_thermostat
    name: xxx
    heater: switch.xxx
    target_sensor: sensor.xxx_temperature
    min_temp: 15
    max_temp: 25
    ac_mode: false
    target_temp: 10
    cold_tolerance: 0.3
    hot_tolerance: 0
    min_cycle_duration:
      seconds: 30
    initial_hvac_mode: "off"
    away_temp: 10
    precision: 0.3

Then an automation to set the temperature

alias: New Automation
description: ''
mode: single
trigger:
  - platform: time
    at: '8:00'
condition:
  - condition: zone
    entity_id: device_tracker.xxx
    zone: zone.home
action:
  - service: climate.set_temperature
    target:
      entity_id: climate.xxx
    data:
      temperature: 19
      hvac_mode: heat

Or instead of the automations use s scheduler card

Using climate entities means you can display them nicely in the frontend too, rather than an automation controlling a switch that’s hard to tell what is happening or to adjust.

gonna try that, but I’va already tried the scheduler card, and it doen not seem to work well before.
Many thanks for the example.

What couldn’t it do. It seems polished, almost perfect

just notice that you have 3 icon on your thermostat (off, fire, and schedule) what thermostat card did you set, because mine does not have the schedule icon