Consider this trigger-based template sensor:
template:
- trigger:
- platform: state
entity_id: sensor.x
sensor:
- name: z
state: >-
... use trigger.to_state.state ...
I learnt that using “trigger.to_state.state” variable is better than using this:
state: >-
... use states('sensor.x') ...
State objects are not 100% clear for me so I wonder - will these 2 variants give same results?
And here is the next question - consider a trigger with 2 entities:
template:
- trigger:
- platform: state
entity_id:
- sensor.x
- sensor.y
How shall I address them?
This way?
template:
- trigger:
- platform: state
entity_id:
- sensor.x
- sensor.y
sensor:
- name: z
state: >-
... use states('sensor.x'), states('sensor.y') ...
Or this way?
template:
- trigger:
- platform: state
entity_id:
- sensor.x
- sensor.y
action:
- variables:
VALUE_X: >-
{% if trigger.entity_id == 'sensor.x' -%}
{{ trigger.to_state.state }}
{%- else -%}
{{ states('sensor.x') }}
{%- endif %}
VALUE_Y: >-
{% if trigger.entity_id == 'sensor.y' -%}
{{ trigger.to_state.state }}
{%- else -%}
{{ states('sensor.y') }}
{%- endif %}
sensor:
- name: z
state: >-
... use VALUE_X, VALUE_Y ...
Made a quick test - it shows that both ways (“trigger.to_state.state” and “states(…)”) give same result…