[SOLVED] Automation and Conditions based on time-state range

Hi guys! :smiley: I’m a newbie and I’ve some troubles with my configuration.

Every second, I’d like to set a new temperature based on an user input value, if time-range conditions are true.
See my code below, here just some informations about inputs:

  • in input_number.myownheater_temperature are saved a new temperature

  • input_datetime.myownheater_time_from and input_datetime.myownheater_time_to are time-only inputs

    alias: MyOwnHeater
    trigger:
    - seconds: /1
      platform: time
    condition:
      - condition: template
        value_template: '{{ strptime(states.input_datetime.myownheater_time_from, "%H-%i") <= strptime(now(), "%H-%i") }}'
        - condition: template
          value_template: '{{ strptime(now(), "%H-%i") < strptime(states.input_datetime.myownheater_time_to, "%H-%i") }}'
    action:
    - data_template:
        entity_id: climate.myownheater
        temperature: '{{ states.input_number.myownheater_temperature.state }}'
      service: climate.set_temperature
    - data: {}
      service: climate.turn_on
    

In example, I’d like to set 21°C and turn on automatically my own heater from 10:00pm to 12:00pm, so:

  • input_number.myownheater_temperature contains 21.0°C
  • input_datetime.myownheater_time_from contains 10:00pm
  • input_datetime.myownheater_time_to contains 12:00pm

Can you help me, please? :smiley:

Do you get any errors when the automation fires? I’m not sure if it was a typo or not, but the indenting in your conditions looks wrong.

Nope, no errors in configuration check or after reboot, but this automation is never trigger.

I don’t know if it’s required, but when I use time triggers, I put the “/1” in quotes.

I’ve modified automation with “/1” and I’ve also changed my conditions template from “%H-%i” to “%H:%M”, according to strptime() format, but it’s not triggered.

If I configure my automation.yaml with explicit times it works perfectly, in example with ‘10:00’ or ‘12:00’ :frowning:

Ahhh… that helped point me in the right direction. I was missing it too.

states.input_datetime.myownheater_time_to
needs to be
states.input_datetime.myownheater_time_to.state

and

states.input_datetime.myownheater_time_from
needs to be
states.input_datetime.myownheater_time_from.state

Nothing to do…I think that conditions are written in a wrong way (semantically), but I don’t know how to fix them.
Here the updated configuration:

alias: MyOwnHeater
trigger:
 - seconds: "/1"
   platform: time
condition:
  - condition: template
    value_template: '{{ strptime(states.input_datetime.myownheater_time_from.state, "%H:%M") <= strptime(now(), "%H:%M") }}'
  - condition: template
    value_template: '{{ strptime(now(), "%H:%M") < strptime(states.input_datetime.myownheater_time_to.state, "%H:%M") }}'
action:
  - data_template:
    entity_id: climate.myownheater
    temperature: '{{ states.input_number.myownheater_temperature.state }}'
    service: climate.set_temperature
 - data: {}
   service: climate.turn_on

Indentation is incorrect in your first action.

You have no service data for your second action.

action:
  - data_template:
      entity_id: climate.myownheater
      temperature: '{{ states.input_number.myownheater_temperature.state }}'
    service: climate.set_temperature
 - data:
     some_key_here: #entity_id most likely, or something, and probably a parameter or two.
   service: climate.turn_on

I also suspect that the actions should be the other way around?

Try this…

alias: MyOwnHeater
trigger:
 - seconds: "/1"
   platform: time
condition:
  - condition: template
    value_template: >-
      {{ as_timestamp(now()) | timestamp_custom('%H%M') | int < state_attr('input_datetime.myownheater_time_to','timestamp') | timestamp_custom('%H%M') | int }}
  - condition: template
    value_template: >-
      {{ state_attr('input_datetime.myownheater_time_from','timestamp') | timestamp_custom('%H%M') | int <= as_timestamp(now()) | timestamp_custom('%H%M')  }}
action:
  - data_template:
    entity_id: climate.myownheater
    temperature: '{{ states.input_number.myownheater_temperature.state }}'
    service: climate.set_temperature
 - data: {}
   service: climate.turn_on

Your last solution doesn’t work, unluckily… but I had found a solution!! :smiley:
It’s composed by 2 step:

  • first, I checked the value of “time_zone” in configuration.yaml and it was not correct (now it’s Europe/Rome and it’s correct)
  • second, after some logs, I found a very useful example that solved my problem (you can see it in the code below)

This is my last version of code in automations.yaml:

- alias: "MyOwnHeater"
  trigger:
  - seconds: '/1'
    platform: time
  condition:
 - condition: template
    value_template: >
     {% set ts = states.input_datetime.myownheater_time_from.attributes.timestamp %}
     {% set midnight = as_timestamp("%d-%d-%d 00:00:00" | format(now().year, now().month, now().day)) %}
     {{ now().strftime("%H:%M") >= (midnight + ts) | timestamp_custom("%02H:%02M", 1) }}
  - condition: template
    value_template: >
     {% set ts = states.input_datetime.myownheater_time_to.attributes.timestamp %}
     {% set midnight = as_timestamp("%d-%d-%d 00:00:00" | format(now().year, now().month, now().day)) %}
     {{ now().strftime("%H:%M") < (midnight + ts) | timestamp_custom("%02H:%02M", 1) }}
    action:
  - data_template:
      entity_id: climate.myownheater
      temperature: '{{ states.input_number.myownheater_temperature.state | float }}'
    service: climate.set_temperature
  - data:
      entity_id: climate.myownheater
    service: climate.turn_on

Thank you very much for your support! :smiley:

Ahhh… so it had to do with the value stored in the input_datetime entities. Makes sense. I’ve never used those, so I was approaching it from the other side.

Also worth noting… with a time trigger of “/1” seconds, this trigger is literally running every single second. No matter what you run Home Assistant on, this is surely a waste of CPU.

You can change the trigger on this to save some CPU, and add a second trigger to get an instant effect if someone changes the input_number.

Outside of that, every 5 minutes seems pretty reasonable.

So, triggers, maybe, like this instead…

trigger:
  - platform: state
    entity_id: input_number.myownheater_temperature
  - platform: time:
    second: '00'
    minutes: '/5'

Either way, I’m glad you found a working solution.

Ah ok, extending running time is a good suggestion :wink:
I think that I’ll keep a real-time feedback from Hass.io during studing and automations developing phases and I’ll change to 5 minutes the trigger in production mode.
Thank you very much!