Running 0.74.2
I’m trying to understand what is the available scope for the various variables in the configuration yaml.
I’m receiving Invalid service data for notify.zohar_sms in a message I’m sending as a notification and I’m wondering if it’s related somehow to variables scope.
- alias: Send message with waze info
initial_state: True
trigger:
# make sure the trigger is fired at an expected event
platform: homeassistant
event: start
action:
service: notify.zohar_sms # Based on free_mobile sms platform. Tested.
data:
# message: "Regular text messages work perfectly"
# message: "Text with variable such as {{ sensor.waze_route_info.origin }} or {{ waze_route_info.destination }} are not working"
message_template: "Same with message_template: Text with variable such as {{ sensor.waze_route_info.origin }} or {{ waze_route_info.destination }} are not working"
Note the messages on the buttom - only pure text messages are actually sent. For the rest I’m receiving Invalid service data for notify.zohar_sms error in the log.
A few questions:
What is the expected variable scope in Hass’s config files?
the only templating sections that exist are service_template, data_template, and value_template. Message_template is something you made up. This is the correct syntax:
- alias: Send message with waze info
initial_state: True
trigger:
platform: homeassistant
event: start
action:
# You should consider adding a delay. If waze doesn't respond instantly on startup,
# all the attributes could not exist yet.
service: notify.zohar_sms
data_template: #<---- changed to data_template
message: "Text with variable such as {{ sensor.waze_route_info.origin }} or {{ waze_route_info.destination }} are not working"
@petro Thanks a lot for that pointer. I’ve changed accordingly. I read the docs 50 times and still missed that bit.
So the syntax is now corrected, but there still remains the question about variable scope, or object scope.
The error I’m getting now is that Error rendering data template: UndefinedError: 'sensor' is undefined
(This is why @finity 's suggestion didn’t work either - the problem is at the object level )
If origin is an attribute of sensor.waze_route_info, then you need to replace sensor.waze_route_info.origin with state_attr('sensor.waze_route_info', 'origin').
#EDIT: It is possible that it has origin and destination inherited from the base class though. Honestly, I don’t remember and I don’t have it configured to verify.
My sensor has the origin and destination attributes (it has to, so waze can calculate the driving time…)
Or maybe the term attribute is the misleading one?
This brings me back to the scope issue: how do I access the origin/destination attributes, which are part of the sensor, but not part of the calculate object? (where the duration and route are given)
Many thanks to all of you who are in to clarifying this issue!
See especially the device_state_attributes method.
@petro, you’re right, the only entity attributes are the four you listed, not origin or destination.
The term “attribute” is definitely overloaded in HA. In general, we mean an entity’s attributes, as in states.domain.object_id.attributes.xxx or state_attr(entity_id, 'xxx'), and not a class object’s attributes in the Python implementation. You can see which “attributes” an entity_id has by looking on the States page.
So why do you want that information? I can make you a custom component with that info, but is there a reason why you want the origin and destination? If you make a static origin and destination, then you already know it depending on the sensor it is coming from.
for example, you can name the sensor: Work travel time. Then have the message say “Your Work travel time is XXX”
EDIT: Also, looking at the code, it’s going to be a larger change than I thought. The origin and destination isn’t always populated because it can come from another entity. Sometimes its a zone with latitude and longitude.
@petro I don’t really need this for anything practical - I’m doing my first steps with HA and I chose the Waze component as an exercise. My goal was to send myself a text message that says something like “Going from $origin to $destination takes $duration minutes” or similar.
By doing it I expected to achieve several things:
Get to know the HA configuration structure better
Send a basic alert to my phone
Create a smart condition to learn how conditions work in HA (e.g. if travel time is less than X minutes then send alert)
So I guess a more suitable question would be how do I reuse the sensor’s configuration as part of my message template. I may go ahead and create a new thread with a more precise question regarding the use, reuse and scope of variables and entities in HA.
The short answer is, you can’t. What you’re looking for is part of this platform’s implementation, and not part of what the platform author chose to expose to the end user.
Mainly you can see the state of entities via state objects, which I pointed you to before. Go to the States page in the frontend, and click on sensor.waze_route_info. Then at the top of the page you will see its state, and its attributes (both the attribute names and their current values.) It will look something like this:
If you don’t see it there, then it wasn’t made available by the component and platform code.
I have to update the component at some point to bring it in line with the new ‘format’ that all new components are required to have. When I make that change I’ll add the 2 attributes. But I don’t have time for it anytime soon. In the meantime. The waze travel time app allows for users to use Zones as origins and destinations. So in theory, you could configure 2 zones:
zone:
- name: Paris France
latitude: 48.864716
longitude: 2.349014
radius: 500
- name: Berlin Germany
latitude: 52.520008
longitude: 13.404954
radius: 500
sensor:
- name: waze_route_info
platform: waze_travel_time
origin: zone.paris_france
destination: zone.berlin_germany
region: 'EU'
automation:
- alias: Send message with waze info
initial_state: True
trigger:
platform: homeassistant
event: start
action:
# You should consider adding a delay. If waze doesn't respond instantly on startup,
# all the attributes could not exist yet.
service: notify.zohar_sms
data_template: #<---- changed to data_template
message: "The travel time from {{ states.zone.paris_france.name }} to {{ states.zone.berlin_germany.name }} is {{ states.sensor.waze_route_info.attributes.duration }}.