Parsing variable in data_template

Hello,
How do I use the variable with name hours? Thank you.

Error
ERROR (MainThread) [homeassistant.core] Invalid service data for input_boolean.turn_on: Entity ID is an invalid entity id for dictionary value @ data['entity_id']. Got ''

  script_hours:
      sequence:
        - service: input_boolean.turn_on    
          data_template: 
            entity_id: >
              {% set original ="{{ hours }}" %}
              {% set space = original.replace(' ', '') %}
              {% set right_bracket = space.replace('[', '') %}
              {% set final_hours = right_bracket.replace(']', '') %}
              {% for word in final_hours.split(',') %}
              {%- if '3' in word -%}
              input_boolean.statusboiler
              {%- endif -%}
              {% endfor %}

I’m pretty sure just:

{% set original = hours %}
1 Like

What happens if 3 isn’t in word at the end of all that? Looks like nothing is returned which will cause an error.

Also where is the variable “hours” coming from?

1 Like

Yes, thank you very much.

3 is only for test. Yes, if word isn’t 3 => error.

you could also just replace everything at once unless you are trying to keep the lines short…

It’s not any ‘better’, just easier to read.

    {% for word in hours.replace('[','').replace(']','').replace(' ','').split(',') %}
    {%- if '3' in word -%}
    input_boolean.statusboiler
    {%- endif -%}
    {% endfor %}

Yes, thank you.

I don’t know how to prevent the error.

Not knowing the full context, an easy way would be to create a “dummy” input_boolean that you don’t actually use for anything or show in the frontend. Then, if the value of hours doesn’t lead to a desired input_boolean, you could have it turn on the dummy one. E.g.:

    {% for word in hours.replace('[','').replace(']','').replace(' ','').split(',') %}
    {%- if '3' in word -%}
    input_boolean.statusboiler
    {%- else -%}
    input_boolean.dummy
    {%- endif -%}
    {% endfor %}

Shouldn’t most of this code be in the condition as a template, such that if the code fails it doesn’t try to turn anything on?

1 Like

Here is problem, I need this:

foreach (hour in hours)
{
    if(actual_hour == hour)
    {
        input_boolean = true;
        return;
    }
    
    input_boolean = false;
}

Ah, ok, try using a service template. Also, you have to dump the for loop.

  script_hours:
      sequence:
        - service_template: >
            {% set my_hours = hours.replace('[','').replace(']','').replace(' ','').split(',') %}
            {% if now().hour | string in my_hours %}
            input_boolean.turn_on
            {% else %}
            input_boolean.turn_off
            {%- endif -%}
          entity_id: input_boolean.statusboiler
1 Like

The best solution, thank you. :slight_smile:

Is it possible create script with paramater hours, which return true/false and this script call in condition in automation?

Eh, there are ways of getting that, but it wouldn’t be a script it would be a template sensor. Then you’d run into issues using the now() function because it doesn’t update template sensors.

To get the template sensor working, you’d need to use the datetime sensor platform:

with this in your sensor config (Remember, you’d need to combine this with your existing sensor section, and combine the platform:templates with your existing platform: templates):

sensor:
  - platform: time_date
    display_options:
      - 'time'

  - platform: template
    sensors:
      correct_hour:
        value_template: >
          {% set my_hours = hours.replace('[','').replace(']','').replace(' ','').split(',') %}
          {{ strptime(states.sensor.time.state, '%I:%M').hour | string in my_hours }}

Then, you’d use the sensor.correct_hour in your condition as being True or False.

Where is the parameter hours pass?

Yeah, i’m an idiot. I forgot about that part. Where are you getting hours from?

states.input_text.iframe.state

Oh ok…

sensor:
  - platform: time_date
    display_options:
      - 'time'

  - platform: template
    sensors:
      correct_hour:
        value_template: >
          {% set my_hours = states.input_text.iframe.state.replace('[','').replace(']','').replace(' ','').split(',') %}
          {{ strptime(states.sensor.time.state, '%I:%M').hour | string in my_hours }}
Is it correct syntax for get status in automation?

 condition: 
   condition: template
   value_template:  '{{ states(sensor.correct_hour) }}'