Automation

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.

Thanks for that information, then how to manage minutes? by adding “and” ?

Add another template condition:

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

You could combine the two with ‘and’ but that’s up to you.

I get error when start homeassistant :

ERROR:homeassistant.util.yaml:while scanning for the next token
found character ‘%’ that cannot start any token

then you have made a type error.
maybe a ( or ) to little?

Just as an observation, this case is pretty simple from an automation perspective, yet it seems like the collective does not really know how to actually make it work.

I like home assistant, but it seems like getting just a bit of automation beyond the basics seems complicated and approached almost like witchcraft.