Value_template for platform time (or: change time trigger via GUI)

Hi everybody,

I am trying to create a rule that will turn off devices / groups at a particular time (regardless of sun, presence, etc.; if it is time x, turn them all off). However, I want people to be able to edit this time when logged in rather than manually doing so in my .yaml files.

input_datetime:
  only_time:
    name: "Uhrzeit Abends"
    has_date: false
    has_time: true
    icon: mdi:clock

This creates states.inpuit_datetime.only_time. I can query the set value in templates via {{ states.input_datetime.only_time.attributes.hour}}:{{ states.input_datetime.only_time.attributes.minute}} (which will return 20:15 in this case.

I then added

- id: 'abends0001'
  alias: "Abends Geräte ausschalten"
  trigger:
    platform: time
    value: > # tried countless variations of this, this is just the most recent one
      "{{states.input_datetime.only_time}}" 
    # at: '18:00:00'
  condition: []
  action:
    - service: switch.turn_on
      entity_id: group.abends

I also tried value_template, but that did also not work.

So basically, this value will most likely not change very often. However, it would still be nice to have the ability to do so on the go rather than having a hard-coded value that I could still edit on the go (vpn => ssh => vim), but not as comfortably. This attempt should create an input box to set a certain time (which it did!) and then use that value for the automation (which I cannot figure out how to do).

Is this possible, and if so, what should I read up on to understand what I should change?

Thanks in advance for your help!

You can not template that value.

You will have to use a template trigger: https://www.home-assistant.io/docs/automation/trigger/#template-trigger

1 Like

Thank you @tom_l

I tried this yesterday, but couldn’t get it to work. I tried under templates =>

{% if states.sensor.time.stat == states.input_datetime.only_time.state | replace(':00','') %}
 true
{% else %}
 false
{% endif %}

sensor.time.stat would provide something like 19:45, while states.input_datetime.only_time would use format 19:45:00, so I tried to strip it off the part it doesn’t need. But it would always report true, even when I set the input to the current time.

This is probably on me, because I have hardly any experience with jinja2 and maybe I’ll have to convert both states to int or something for this to work… but is this even the right direction, or am I trying something here that won’t work or could be solved way more simple?

This is the way I do it:

  trigger:
    platform: template
    value_template: "{{ states('sensor.time') == states('input_datetime.lounge_dehumidifier_run_time').rsplit(':',1)[0] }}"
1 Like

Thank you @tom_l
I had removed this attempt from my configuration , but I will try again this weekend and see if that works for me. It is still a bit difficult for me to wrap my head around jinja2.

Like everything it gets easier with practice. I’m still making basic mistakes after a year though!

1 Like