Hi All,
I’m using Google Location Sharing, and I’m working on building a trigger that will message my users to tell them to plug their phones in when they get to a certain battery level. I’ve successfully captured the data with a sensor. However, I wanted to see if there may be a way to do this a little more “cleanly”, if you will. Here’s my current code:
now, the problem I run into is, I have 6 users. in order to do this, the most straightforward way is to build a sensor for each, for each of the attributes that I need to check. This can get crazy, quick.
Is there anyway that I can provide a list of the device trackers ID’s, and then apply that to the code with a variable in the code, sort of like doing a function? Sort of like this:
I think I get it. You want to create multiple sensor entities (one per user) that are all based on the same ‘skeleton’ of a template_sensor. Effectively, you want to do ‘code re-use’. All six sensors are identical except for the user’s name so why not define the skeleton of a sensor once and then just pass it the name of the user?
As far as I know, you can’t do that.
There’s a way in YAML to create a shareable block that can be included elsewhere (many times). See YAML Anchors, References, Extend. However, as far as I can see, it’s not applicable in this case. There’s just not enough in that template_sensor to merit its use.
There’s a frighteningly long thread (that I’ve promised myself to read … but never have) that might be applicable to you:
Precisely what I mean; from a general coding/math idea it would be creation of a function (I’m a little embarrassed at how long it took me to remember the right word for it )
The long thread you mentioned looks like it may be a pretty strong candidate for a fix, but ideally when it’s done I’d like this to fire off a message to the individual users, rather than just me. Still it gives me an example of what can be done with this code, to tear apart and tinker with. Thanks!!
FWIW, you can pass variables to python scripts but I’m not sure that’s of any help for your use-case.
Minimally, you need an automation that’s triggered when any one of the six phone batteries drops below a threshold. Then it reports which phone battery is below the threshold.
The end result is the battery percentage to the right, with an icon that changes based on whether or not it’s plugged in and what percentage of charge it has.
What is the rest of your automation? I.e., the condition and action parts. I suspect the error is coming from there (notice it says “data template”), since that trigger should be good.
I added the second trigger to test that action but I just receive the google message. The notify service is working (tested in another automation)
Thanks a lot
That’s because when you put the message into google_say disconnected from the automation it’s originally connected to there is no “trigger.to_state”…
what that bit of code does is it creates a placeholder in the template that looks for the entity that caused the automation to trigger along with the new state of the entity which then allows the template to access the associated attributes of the triggering entity.
try putting the following into the tts and see what the result is (using the entity_id of the example friendly name you used last above):