Hey everybody,
my goal is to replace static number in value template with input_number, so that I could pass values to sensor from UI.
E.g. I have this template:
value_template: >
{%- set numberOfSequentialHours = 3 -%}
{%- set lastHour = 23 -%}
{%- set firstHour = 0 -%}
where numberOfSequentialHours is set to 3. I want it to be set to whatever input_number helper holds, i.e.:
value_template: >
{%- set numberOfSequentialHours2 = "{{ states('input_number.sequential_hours') | int }}" -%}
{%- set lastHour2 = 23 -%}
{%- set firstHour2 = 0 -%}
Can’t get it to work.
tom_l
2
Everything you have there is correct. Try sharing the entire sensor config.
- platform: template
sensors:
cheapest_custom_hours_energy_tomorrow:
unique_id: t24psb/nxp5qR+XqPdp8CtpJo6Wq5tJjvergdfLRxGg=
device_class: timestamp
friendly_name: Cheapest custom sequential electricity hours
value_template: >
{%- set numberOfSequentialHours = "{{ states('input_number.sequential_hours') | int }}" -%}
{%- set lastHour = 23 -%}
{%- set firstHour = 0 -%}
{%- if state_attr('sensor.nordpool_kwh_lt_eur_3_10_021', 'tomorrow_valid') == true -%}
{%- set ns = namespace(counter=0, list=[], cheapestHour=today_at("00:00") + timedelta( hours = (24)), cheapestPrice=999.00) -%}
{%- for i in range(firstHour + numberOfSequentialHours, lastHour2+1) -%}
{%- set ns.counter = 0.0 -%}
{%- for j in range(i-numberOfSequentialHours22, i) -%}
{%- set ns.counter = ns.counter + state_attr('sensor.nordpool_kwh_lt_eur_3_10_021', 'tomorrow')[j] -%}
{%- endfor -%}
{%- set ns.list = ns.list + [ns.counter] -%}
{%- if ns.counter < ns.cheapestPrice -%}
{%- set ns.cheapestPrice = ns.counter -%}
{%- set ns.cheapestHour = today_at("00:00") + timedelta( hours = (24 + i - numberOfSequentialHours)) -%}
{%- endif -%}
{%- endfor -%}
{{ ns.cheapestHour }}
{%- set ns.cheapestPrice = ns.cheapestPrice / numberOfSequentialHours -%}
{%- endif -%}
If I put any number instead of “{{ states(‘input_number.sequential_hours’) | int }}” - it works.
tom_l
4
What does this return in the template editor?
{{ states('input_number.sequential_hours') | int }}
tom_l
6
Try this:
{%- set numberOfSequentialHours = states('input_number.sequential_hours') | int -%}
If I don’t put quotes, I can’t pass validation:
Configuration invalid!
Invalid config for [sensor.template]: invalid template (TemplateSyntaxError: expected token ':', got '}') for dictionary value @ data['sensors']['cheapest_custom_hours_energy_tomorrow']['value_template']. Got '{%- set numberOfSequentialHours = {{ states(\'input_number.sequential_hours\') | int }} -%} {%- set lastHour = 23 -%} {%- set firstHour = 0 -%}\n{%- if state_attr(\'sensor.nordpool_kwh_lt_eur_3_10_021\', \'tomorrow_valid\') == true -%}\n {%- set ns = namespace(counter=0, list=[], cheapestHour=today_at("00:00") + timedelta( hours = (24)), cheapestPrice=999.00) -%}\n {%- for i in range(firstHour + numberOfSequentialHours, lastHour2+1) -%}\n {%- set ns.counter = 0.0 -%}\n {%- for j in r.... (See ?, line ?).
Doesn’t work. Although it passes validation, sensor state is unavailable using this.
tom_l
9
I just tried this in the template editor (using one of my input numbers). It works fine:
{%- set numberOfSequentialHours = states('input_number.rack_fan_dwell_time') | int(0) -%}
{{ numberOfSequentialHours }}
Many thanks, buddy. Got it working.
Cheers