Automation with variable start time

Hi all,
i’m very new in the Home Assistant world and in this blog. I read already a lot here but this is my first post. I hope i don’t miss a important information and you can help me.

Since some time i try to get an automation working. Until now without success. You see my code below. I think the problem is the format of the sensor. I experimented already with some formats but i did not get it to run. I would be very happy if you can give a recomendation how to fix it :slight_smile:

Here are the input_number components i’m using in configuration.yaml:

input_number:
  vacuum_start_hour_monday:
    name: Montag (h)
    min: 0
    max: 23
    step: 1
    unit_of_measurement: Hour  
    icon: mdi:clock
  vacuum_start_minute_monday:
    name: Montag (min)
    min: 0
    max: 55
    step: 5
    unit_of_measurement: Minute  
    icon: mdi:clock-outline

Here are the sensor components i’m using in configuration.yaml:

sensor:
  - platform: template
    sensors:
      vacuum_start_monday:
        friendly_name: Startzeit Montag
        icon_template: mdi:timer
value_template: '{{ "%0.02d:%0.02d" | format(states.input_number.vacuum_start_hour_monday.state | int, states.input_number.vacuum_start_minute_monday.state | int ) }}'

Here is my automation in automations.yaml:

- id: roborock_cleaning
  trigger:
  - platform: time_pattern
    minutes: '/1'
  condition:
  - condition: template
    value_template: '{{ now().time().strftime("%R") == sensor.vacuum_start_monday }}'
  action: 
  - service: notify.ha_message
    data:
      message: 'Automation active'

At first glance I noticed the spacing is off in your second code block. Since all of your other spacing looks right I’m guessing it might have gotten messed up while formatting your post, but just in case:

sensor:
  - platform: template
    sensors:
      vacuum_start_monday:
        friendly_name: Startzeit Montag
        icon_template: mdi:timer
        value_template: '{{ "%0.02d:%0.02d" | format(states.input_number.vacuum_start_hour_monday.state | int, states.input_number.vacuum_start_minute_monday.state | int ) }}'

Thank you for the response.
Your right. That happend during formatting the post. My fault - sorry. In my code i have the variant you have written. Therefore, that is not the solution. Do you see something else what could be wrong?

1 Like

I think the value_template in the condition should be:

value_template: '{{ now().time().strftime("%R") == states.sensor.vacuum_start_monday.state }}'
1 Like

Did you create the template sensor sensor.vacuum_start_monday because you use it in the automation and to display the scheduled time in the UI?

If you do not need to display the scheduled time in the UI, then the template sensor can be removed and I can show you how to simplify the automation’s template.

Thank you for the response. I tried a little bit with the suggestion from VDRainer but it did not work.

And yes i use the template sensor to show the active time in the ui. Therefore i would like to keep it if possible. But at the moment it is not working at all. So i’m happy with every suggestion.

Got it working now. During the tests i made an error in the action part. Therefore the complete correct hint from VDRainer does not work when i tested it first time.

Thank you very much for the help! :grinning:

To be clear - this is the solution for me:
value_template: '{{ now().time().strftime("%R") == states.sensor.vacuum_start_monday.state}}'

Out of curiosity, why did you choose to use two input_numbers, to set the time, instead of one input_datetime? It displays the time and lets you adjust it.

Appearance in UI:
Screenshot%20from%202019-04-14%2009-24-22

Appearance in UI when clicked:

Screenshot%20from%202019-04-14%2009-24-42

Was it for the aesthetics of setting the time using sliders?