Automation

hello, I made automation triggered by state of input boolean and time condition :

automation:
  - alias: 'turn on test'
    trigger:
      platform: state
      entity_id: input_boolean.timer
      to: 'ON'
    condition:
      condition: time
      after: '08:55:00'
    action:
      service: input_boolean.turn_on
      entity_id: input_boolean.output

the problem is, why automation not triggered at certain time (‘08:55’) if I turn on input_boolean.timer before ‘08:55’, let say at ‘08:50’?
How to solve it?

you need just a small change

automation:
  - alias: 'turn on test'
    trigger:
      platform: state
      entity_id: input_boolean.timer
     from: 'off'
     to: 'on'
    condition:
      condition: time
      after: '08:55:00'
    action:
      service: input_boolean.turn_on
      entity_id: input_boolean.output

I do not think the suggestion from @ReneTode will solve your problem.

Try something like:


automation:
  - alias: 'turn on test'
  trigger:
    - platform: state
      entity_id: input_boolean.timer
      to: 'ON'
    - platform: time
      after: '08:55'
  condition:
    - condition: time
      after: '08:55:00'
    - condition: state
      entity_id: input_boolean.timer
      state: 'on'
    action:
      service: input_boolean.turn_on
      entity_id: input_boolean.output

Thanks for your support, so automation will check the time at ‘08:55’ and condition.
Another question, is state from a variable, let say sensor.alarm_time (value is 08:55) cannot be parsed into time trigger configuration?

trigger:
   platform: time
   after: '{{ states(sensor.alarm_time) }} '

@Danielhiversen you are right i just saw his code and didnt read his question right.

but i think you need from: off to: on (but im not sure)
and dont you need condition: AND in your example?

@cyber.mocca no you cant use template values in after:

I’m not sure if we need to add “from:…” but I’m agree to use condition: and.

So, how to manage a system that need a user-defined time as time trigger?

look at the alarmclock topics here on the forum, i think they will be helpfull

I have tried creating alarm clock topic, but it doesnt work for me
The trigger is to parsing value template to now.time().srtftime(), but till the certain time that user defined, the message doesnt appeared

off course the message only appears when the alarm is triggered.
but you could add a second trigger in the automation, like @Danielhiversen showed.

condition: AND is default, so you do not need to add that.

1 Like

Yes, thanks for your suggestion. But till now, this code doesnt work for me.
Anything wrong with this?

input_slider: 
  alarmhour:
    name: Hour
    icon: mdi:timer
    initial: 9
    min: 0
    max: 23
    step: 1
  alarmminutes:
    name: Minutes
    icon: mdi:timer
    initial: 0
    min: 0
    max: 55
    step: 5

input_boolean: 
  alarmstatus:
    name: Wake Me Up
    initial: off
    icon: mdi:alarm-check
  alarmweekday:
    name: Weekdays Only
    initial: off
    icon: mdi:calendar

sensor: 
  platform: template
  sensors:
    alarm_hour:
      friendly_name: 'Hour'
      value_template: '{{ states("input_slider.alarmhour") | round(0) }}'
    alarm_minutes:
      friendly_name: 'Minutes'
      value_template: '{{ states("input_slider.alarmminutes") | round(0) }}'
    alarm_time:
      friendly_name: 'Time'
      value_template: '{{ states("sensor.alarm_hour") }}:{% if states("sensor.alarm_minutes")|length == 1 %}0{% endif %}{{ states("sensor.alarm_minutes") }}'

group:
  default_view:
    view: yes
    entities:
      - group.alarmclock
  alarmclock:
    name: Wake Me Up
    entities: 
      - sensor.alarm_time
      - input_slider.alarmhour
      - input_slider.alarmminutes
      - input_boolean.alarmstatus
      - input_boolean.alarmweekday

automation
  - alias: 'Wake Me Up'
    trigger:
      platform: template
      value_template: '{{ now.time().strftime("%-H") == states.sensor.alarm_hour.state and now.time().strftime("%-M") == states.sensor.alarm_minutes.state }}'
    condition:
      condition: or
      conditions:
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarmstatus
              state: 'on'
            - condition: state
              entity_id: input_boolean.alarmweekday
              state: 'on'
            - condition: time
              weekday:
                - mon
                - tue
                - wed
                - thu
                - fri
        - condition: and
          conditions:
            - condition: state
              entity_id: input_boolean.alarmstatus
              state: 'on'
            - condition: state
              entity_id: input_boolean.alarmweekday
              state: 'off'
    action:
      service: notify.notify
      data:
        message: 'Good morning. Time to Wake Up!'
        title: ""

i think that strftime("%-H") should be strftime("%H")
same for M

Yes, but still not working… :frowning:
I think because automation triggered by template changing, not time.
Any suggestion?

in the template value you compare a string with a rounded float.
“09”==9 will never be right

{{ (now.time().strftime("%H")|int == states.sensor.alarm_hour.state|int) and (now.time().strftime("%M")|int == states.sensor.alarm_minutes.state|int) }}

this could do it (at least that template comes to true)

I’ll try that, so my automation be like this :

automation:
  - alias: 'turn on test'
    trigger:
      platform: template
      value_template: '{% if is_states("sensor.alarm_hour|int", "now.time().strftime("%H:%M")|int")}true{% endif %}'
    condition:
      condition: state
      entity_id: input_boolean.timer
      state: 'on'
    action:
        service: persistent_notification.create
        data:
            message: '{{states("sensor.alarm_time")}}'
            title: "message"

the automation should work, the message not i think.
i think the message should be a value_template

Still dont work.
I’m confusing for that code, I changed trigger to time trigged every 5s.
And check condition, if true then action.

automation:
  - alias: 'turn on test'
    trigger:
      platform: time
      hours: '*'
      minutes: '*'
      seconds: '/5'
    condition:
      conditions:
         - condition: state
           entity_id: input_boolean.timer
           state: 'on'
         - condition: template
           value_template: '{% if is_state("sensor.alarm_hour|int", "now.time().strftime("%H:%M")|int")%}true{% endif %}'
    action:
        service: persistent_notification.create
        data:
            message: '{{states.sensor.alarm_time.state}}'
            title: "message"

I don’t think this is accepted. If you don’t want to specify hours or minutes, just leave them out.

        data_template:
          message: '{{states.sensor.alarm_time.state}}'
          title: "message"

And for this, I took out 2 indents in front of message and title, and changed data to data_template.

So in total, it shoud look like this (with a few more indents removed):

automation:
  - alias: 'turn on test'
    trigger:
      platform: time
      seconds: '/5'
    condition:
      conditions:
        - condition: state
          entity_id: input_boolean.timer
          state: 'on'
        - condition: template
          value_template: '{% if is_state("sensor.alarm_hour|int", "now.time().strftime("%H:%M")|int")%}true{% endif %}'
    action:
      service: persistent_notification.create
      data_template:
        message: '{{states.sensor.alarm_time.state}}'
        title: "message"

Thanks for your correction about time platform that dont need hours and minutes, but the problem is the condition.
I think the condition template doesnt return “true” for that code.

'{% if is_state("sensor.alarm_hour|int", "now.time().strftime("%H:%M")|int")%}true{% endif %}'

And still not work if I changed to this :

'{% if is_state("sensor.alarm_hour|int", "sensor.worldclock_sensor|int")%}true{% endif %}'

any idea to compare value of template and current time that can return “true” value?

I’m not sure about the second example, but you have too many double quotes going in the first one, cutting your strings short unintentionally. Try this:

- condition: template
  value_template: >
    {% if is_state("sensor.alarm_hour|int", "now.time().strftime('%H')|int")%}true{% endif %}

By putting the template on a new line with >, you can omit the single quotes and use them for your time format instead. And also, you can’t convert something in the format %H:%M to an integer, so just use %H.