I am very sure that it used to work fine, but at some point, it has started throwing an error.
2021-10-23 12:25:18 ERROR (Thread-17) [homeassistant.helpers.template] Template variable error: 'trigger' is undefined when rendering '"{{ (trigger.to_state.state.sensor.garage_door_sensor_x_coordinate|float - trigger.from_state.state.sensor.garage_door_sensor_x_coordinate|float)|abs > 100}}"'
Did I make a mistake somewhere or did something change? I am not expert in template code writing, but whatever I could glean from documentation, trigger.to_state.state.entity_id seems to be a valid syntax.
Thanks
I can’t quite tell what that’s supposed to do, and I’m not at all surprised that it doesn’t work. While trigger.to_state.entity_id is a valid (yet over-complicated) way to get the entity_id of the entity that triggered the automation (see this and this), trigger.to_state.state.sensor.garage_door_sensor_x_coordinate has no meaning. It looks like you glued a trigger variable and an actual entity_id together, and you’ll need to figure out what value you’re actually trying to extract.
What is the meaning of sensor.garage_door_sensor_x_coordinate there? Is that an entity? Are you trying to get the value of an attribute?
I have a Samsung Z-Wave movement sensor which reports changes in x, y and z coordinates when it detects movement. So, I want to find out by how much the coordinates have changed. If either of x or y or z coordinate changes by more than 100 or sensor detects acceleration, I send a notification to my phone.
I am subtracting from_state from to_state to find this. The line is long so not easily readable and easy to miss the - sign.
- id: '1601555516668'
alias: Garage Door Movement
description: The sensor reports x, y and z coordinates only when acceleration is
detected.
trigger:
- platform: state
entity_id: sensor.garage_door_sensor_x_coordinate
- platform: state
entity_id: sensor.garage_door_sensor_y_coordinate
- platform: state
entity_id: sensor.garage_door_sensor_z_coordinate
- platform: state
entity_id: binary_sensor.garage_door_sensor_acceleration
condition:
- condition: or
conditions:
- condition: template
value_template: "{{ (trigger.to_state.state.sensor.garage_door_sensor_x_coordinate|float - trigger.from_state.state.sensor.garage_door_sensor_x_coordinate|float)|abs > 100 }}"
- condition: template
value_template: "{{ (trigger.to_state.state.sensor.garage_door_sensor_y_coordinate|float - trigger.from_state.state.sensor.garage_door_sensor_y_coordinate|float)|abs > 100 }}"
- condition: template
value_template: "{{ (trigger.to_state.state.sensor.garage_door_sensor_z_coordinate|float - trigger.from_state.state.sensor.garage_door_sensor_z_coordinate|float)|abs > 100 }}"
- condition: state
entity_id: binary_sensor.garage_door_sensor_acceleration
state: 'on'
action:
- service: notify.mobile_app_my_phone
data:
message: Check garage door
title: Door moved
mode: single
Basically, whatever entity triggered the automation, you’ll subtract the old value from the new value and see if the absolute value is greater than 100.
Thanks for replying. It will take me some time to implement the automation. I will post the results here. But the original problem still remains (again I am not sure if I am doing the right things here). I pasted the code into Developer Tools → Template and it is still complaining:
Because trigger is undefined in the developer tools AND when you manually run the automation. Trigger only exists, when the automation is ACTUALLY triggered by one of the things you have listed as a trigger for the automation. At that point trigger is populated and for example trigger.to_state.state will contain the new state of the entity that triggered the automation and trigger.from_state.state will contain the state that it was before the change.
The easiest way to test it is to create an input number helper and then add it to the automation as a trigger, and then you can easily change the number in the helper to trigger the automation and check the automation trace to make sure it worked as you expected it to.