Greater and Less than with Time at Night

Hello,

I made a Automation that should turn on and off a smart switch at a given temperature and between a given time. Time and Temp are set in an input.datetime and input.number.

id: "smart_switch_radiator_badkamer_power_on_off_at_target_temperature"
alias: "Smart Switch - Radiator Badkamer Power On/Off at Target Temperature"
description: >-
  Zet de Radiator Badkamer Aan of Uit als de Temperatuur op de Badkamer voor 1 minuut Boven of Onder de Target Temperatuur komt 
  en de optie Power On/Off at Target Temperatuur Aan staat.

trigger:
  - platform: template
    value_template: >-
      {{ states('sensor.time') ==
      (state_attr('input_datetime.smart_switch_radiator_badkamer_power_on_time',
      'timestamp')| int | timestamp_custom('%H:%M', False) ) }}
    id: start_time_target_temperature
  
  - platform: state
    entity_id:
      - >-
        input_boolean.smart_switch_radiator_badkamer_power_on_off_at_target_temperature
    to: "on"
    for:
      hours: 0
      minutes: 0
      seconds: 10
    id: enable_target_temperature
  
  - platform: state
    entity_id:
      - sensor.target_temperature_badkamer_state
    to: "Temperature Below Target"
    for:
      hours: 0
      minutes: 1
      seconds: 0
    id: low_temperature
  
  - platform: state
    entity_id:
      - sensor.target_temperature_badkamer_state
    to: "Temperature Above Target"
    for:
      hours: 0
      minutes: 1
      seconds: 0
    id: high_temperature

action:
  - choose:
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - start_time_target_temperature
              - condition: state
                entity_id: >-
                  input_boolean.smart_switch_radiator_badkamer_power_on_off_at_target_temperature
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 5
        sequence:
          - choose:
              - conditions:
                  - condition: and
                    conditions:
                      - condition: state
                        entity_id: sensor.target_temperature_badkamer_state
                        state: "Temperature Below Target"
                      - condition: state
                        entity_id: switch.smart_switch_radiator_badkamer
                        state: "off"
                sequence:
                  - type: turn_on
                    device_id: 9fd68ababf5bcd3c9ffcb1dfc76bd117
                    entity_id: f9ddeaf40cab46eee52b983474b4b9c7
                    domain: switch
              - conditions:
                  - condition: and
                    conditions:
                      - condition: state
                        entity_id: sensor.target_temperature_badkamer_state
                        state: "Temperature Above Target"
                      - condition: state
                        entity_id: switch.smart_switch_radiator_badkamer
                        state: "on"
                sequence:
                  - type: turn_off
                    device_id: 9fd68ababf5bcd3c9ffcb1dfc76bd117
                    entity_id: f9ddeaf40cab46eee52b983474b4b9c7
                    domain: switch
              - conditions:
                  - condition: state
                    entity_id: sensor.target_temperature_badkamer_state
                    state: "Temperature On Target"
                sequence:
                  - stop: "No action needed."
                    response_variable: "No Action Needed"
      
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - enable_target_temperature
              - condition: state
                entity_id: >-
                  input_boolean.smart_switch_radiator_badkamer_power_on_off_at_target_temperature
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 5
              - condition: template
                value_template: >-
                  {{ states('sensor.time') >=
                  (state_attr('input_datetime.smart_switch_radiator_badkamer_power_on_time',
                  'timestamp')| int | timestamp_custom('%H:%M', False) ) }}
              - condition: template
                value_template: >-
                  {{ states('sensor.time') <=
                  (state_attr('input_datetime.smart_switch_radiator_badkamer_power_off_time',
                  'timestamp')| int | timestamp_custom('%H:%M', False) ) }}
        sequence:
          - choose:
              - conditions:
                  - condition: and
                    conditions:
                      - condition: state
                        entity_id: sensor.target_temperature_badkamer_state
                        state: "Temperature Below Target"
                      - condition: state
                        entity_id: switch.smart_switch_radiator_badkamer
                        state: "off"
                sequence:
                  - type: turn_on
                    device_id: 9fd68ababf5bcd3c9ffcb1dfc76bd117
                    entity_id: f9ddeaf40cab46eee52b983474b4b9c7
                    domain: switch
              - conditions:
                  - condition: and
                    conditions:
                      - condition: state
                        entity_id: sensor.target_temperature_badkamer_state
                        state: "Temperature Above Target"
                      - condition: state
                        entity_id: switch.smart_switch_radiator_badkamer
                        state: "on"
                sequence:
                  - type: turn_off
                    device_id: 9fd68ababf5bcd3c9ffcb1dfc76bd117
                    entity_id: f9ddeaf40cab46eee52b983474b4b9c7
                    domain: switch
              - conditions:
                  - condition: state
                    entity_id: sensor.target_temperature_badkamer_state
                    state: "Temperature On Target"
                sequence:
                  - stop: "No action needed."
                    response_variable: "No Action Needed"
      
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - low_temperature
              - condition: state
                entity_id: >-
                  input_boolean.smart_switch_radiator_badkamer_power_on_off_at_target_temperature
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 5
              - condition: template
                value_template: >-
                  {{ states('sensor.time') >=
                  (state_attr('input_datetime.smart_switch_radiator_badkamer_power_on_time',
                  'timestamp')| int | timestamp_custom('%H:%M', False) ) }}
              - condition: template
                value_template: >-
                  {{ states('sensor.time') <=
                  (state_attr('input_datetime.smart_switch_radiator_badkamer_power_off_time',
                  'timestamp')| int | timestamp_custom('%H:%M', False) ) }}
              - condition: state
                entity_id: switch.smart_switch_radiator_badkamer
                state: "off"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 5
        sequence:
          - type: turn_on
            device_id: 9fd68ababf5bcd3c9ffcb1dfc76bd117
            entity_id: f9ddeaf40cab46eee52b983474b4b9c7
            domain: switch
      
      - conditions:
          - condition: and
            conditions:
              - condition: trigger
                id:
                  - high_temperature
              - condition: state
                entity_id: >-
                  input_boolean.smart_switch_radiator_badkamer_power_on_off_at_target_temperature
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 5
              - condition: template
                value_template: >-
                  {{ states('sensor.time') >=
                  (state_attr('input_datetime.smart_switch_radiator_badkamer_power_on_time',
                  'timestamp')| int | timestamp_custom('%H:%M', False) ) }}
              - condition: template
                value_template: >-
                  {{ states('sensor.time') <=
                  (state_attr('input_datetime.smart_switch_radiator_badkamer_power_off_time',
                  'timestamp')| int | timestamp_custom('%H:%M', False) ) }}
              - condition: state
                entity_id: switch.smart_switch_radiator_badkamer
                state: "on"
                for:
                  hours: 0
                  minutes: 0
                  seconds: 5
        sequence:
          - type: turn_off
            device_id: 9fd68ababf5bcd3c9ffcb1dfc76bd117
            entity_id: f9ddeaf40cab46eee52b983474b4b9c7
            domain: switch

mode: single

It’s a big one haha. But in daytime it is working, but in Night time it is not because the template:

              - condition: template
                value_template: >-
                  {{ states('sensor.time') >=
                  (state_attr('input_datetime.smart_switch_radiator_badkamer_power_on_time',
                  'timestamp')| int | timestamp_custom('%H:%M', False) ) }}
              - condition: template
                value_template: >-
                  {{ states('sensor.time') <=
                  (state_attr('input_datetime.smart_switch_radiator_badkamer_power_off_time',
                  'timestamp')| int | timestamp_custom('%H:%M', False) ) }}

time ON is greater than 23.00(night) and time OFF is smaller than 8.00 (morning)
And that is not possible, because greater than 23.00 cant be smaller than 8.00.

Is there any other way to use a datetime.number ot template in an automation with time?

Also if someone has an idea of making this automation smaller, your welcome :stuck_out_tongue:

use OR for condition

Thought about that also but than i need to make an OR inside a AND that allowed?

Found also this:

              - condition: time
                after: input_datetime.smart_switch_radiator_badkamer_power_on_time
                before: input_datetime.smart_switch_radiator_badkamer_power_off_time

But should this not be in a kind of template?

Like:

"{{ states('input_datetime.smart_switch_radiator_badkamer_power_off_time') }}"

?

You should be able to make OR and AND inside each other.

1 Like