Parsing variable in data_template

close, you need the entity id in quotes. And because of the quotes on the outside, you need to use different quotes. Also, I think it still treats it as ‘on’ / ‘off’, so you should be using is_state

    condition: 
      condition: template
      value_template:  '{{ is_state("sensor.correct_hour","on") }}'

Ok, thank you. Sensor is still on False. Is it need to update, because hours is correct?

Well, what is the state of the sensor.correct_hour? In the states list

Icon of sensor shows False.

Are you getting errors in the log?

What happens when you put

{% set my_hours = states.input_text.iframe.state.replace('[','').replace(']','').replace(' ','').split(',') %}
{{ strptime(states.sensor.time.state, '%I:%M').hour | string in my_hours }}

into the template editor?

Template editor shows False.

what does this show:

{% set my_hours = states.input_text.iframe.state.replace('[','').replace(']','').replace(' ','').split(',') %}
{{ my_hours }}

['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17', '18', '19', '20', '21', '22', '23', '24']

Ok, thats not the problem, the problem must be the state of the date.

what does this give you?

{{ strptime(states.sensor.time.state, '%I:%M').hour }}

In template editor nothing.

It is need to use:

{% set my_hours = states.input_text.iframe.state.replace('[','').replace(']','').replace(' ','').split(',') %}
{% if now().hour | string in my_hours %}
   on
{% else %}
 off
{%- endif -%}

It is correct.

what about

{{ states.sensor.time.state }}

In template editor is actual time.

Ok, so that could a a datetime object. Try this:

{{ states.sensor.time.state.hour }}

It is possible use this version, it is correct.

  {% set my_hours = states.input_text.iframe.state.replace('[','').replace(']','').replace(' ','').split(',') %}
 {{ now().hour | string in my_hours }}

You cannot use that because it will not update in real time because the now() object does not trigger updates to the template.

EDIT: So what would happen is, it would only update when you change input_text.iframe. So for example, if it was 8am, and you hadn’t changed input_text.iframe since 4pm the day before. The sensor would still have the input_text.iframe 4pm sensor state, not the the current time at 8am.

Ok. Did you mean this?

 {% set my_hours = states.input_text.iframe.state.replace('[','').replace(']','').replace(' ','').split(',') %}
 {{ {{ states.sensor.time.state.hour }} | string in my_hours }}

It works

 {% set my_hours = states.input_text.iframe.state.replace('[','').replace(']','').replace(' ','').split(',') %}
 {{ strptime(states("sensor.time"), "%H:%M").hour | string in my_hours }}

Thank you very much for your work. You are the best. :smiley:

1 Like

Last question. Is it possible do obstraction for different source data (states.input_text.iframe.state, states.input_text.iframe2.state, …)?