I create sensor as seen below.
There is template, which generated 2 variables. “state.state” and “state.hours”. I need both variables as two attributes of the sensor.
Actually I created sensor and two attributes. In every attribute is absolutly same template. It is working, but I don’t like, there are two same template scripts just because I don’t know, how to put two variables to two sensor attributes.
Is better way, how to do it?
- name: sorted_market_prices_bpp_1
state: ''
attributes:
state: >
{% if (is_state('input_select.battmgmt_priceplan1_sortedprice_type','cheap')) %}
{% set select = state_attr('sensor.sorted_market_prices', 'asc') %}
{% elif (is_state('input_select.battmgmt_priceplan1_sortedprice_type','expensive')) %}
{% set select = state_attr('sensor.sorted_market_prices', 'desc') %}
{% else %}
{% set select = '' %}
{% endif %}
{% set x=namespace(x=0) %}
{% set state=namespace(state='false',hours='') %}
{% set tod = states('input_datetime.battmgmt_priceplan1_od')[:2]|int %}
{% set tdo = states('input_datetime.battmgmt_priceplan1_do')[:2]|int %}
{% set actual_hour = (now().timestamp() | timestamp_custom('%H') | int) %}
{% for number in range(select|count) %}
{% if (((select [number]['hour'] |int) >= tod) and ((select [number]['hour'] |int) <= tdo)) %}
{% set x.x=x.x+1 %}
{% set state.hours=state.hours~select [number]['hour']~'('~select [number]['price']~') , ' %}
{% endif %}
{% if ((select [number]['hour'] |int) == actual_hour) %}
{% set state.state='true' %}
{% endif %}
{% if (x.x >= states('input_number.battmgmt_priceplan1_sortedprice_hours')|int) %}
{%break%}
{% endif %}
{%endfor%}
{{state.state}}
hours: >
{% if (is_state('input_select.battmgmt_priceplan1_sortedprice_type','cheap')) %}
{% set select = state_attr('sensor.sorted_market_prices', 'asc') %}
{% elif (is_state('input_select.battmgmt_priceplan1_sortedprice_type','expensive')) %}
{% set select = state_attr('sensor.sorted_market_prices', 'desc') %}
{% else %}
{% set select = '' %}
{% endif %}
{% set x=namespace(x=0) %}
{% set state=namespace(state='false',hours='') %}
{% set tod = states('input_datetime.battmgmt_priceplan1_od')[:2]|int %}
{% set tdo = states('input_datetime.battmgmt_priceplan1_do')[:2]|int %}
{% set actual_hour = (now().timestamp() | timestamp_custom('%H') | int) %}
{% for number in range(select|count) %}
{% if (((select [number]['hour'] |int) >= tod) and ((select [number]['hour'] |int) <= tdo)) %}
{% set x.x=x.x+1 %}
{% set state.hours=state.hours~select [number]['hour']~'('~select [number]['price']~') , ' %}
{% endif %}
{% if ((select [number]['hour'] |int) == actual_hour) %}
{% set state.state='true' %}
{% endif %}
{% if (x.x >= states('input_number.battmgmt_priceplan1_sortedprice_hours')|int) %}
{%break%}
{% endif %}
{%endfor%}
{{state.hours}}