Using entity state to construct URL in rest sensor resource

I’m struggling to make something work, and wonder whether I’m trying the impossible. Basically, I’ve created a rest sensor which uses a Google Maps API to turn lon/lat into placenames which I then use in speech. Constructed and tested using hardcoded lon/lat no probem as below (with real data redacted):

I want the sensor to be dynamic, so am trying to figure out how to use icloud lon/lat (available as an entity in HA) as the construct in the URL. It fails though. My code looks like this (again, redacted):

resource: 'https://maps.googleapis.com/maps/api/geocode/json?latlng={{ states.device_tracker.someiphone.attributes.latitude }}{{ states.device_tracker.somesiphone.attributes.longitude }}&key=mykeygoeshere

The error I see in the log is:

17-02-23 17:59:46 ERROR (MainThread) [homeassistant.helpers.template] Error parsing value: list object has no element 0 (value: {

“results” : [],

“status” : “ZERO_RESULTS”

So I am guessing that the URL is being used literally i.e. not replacing the lon/lat with the entity information.

Anyone had any experience of this or know if what I am trying is even possible.

No, but I’m certainly going to watch this to see if you get it figured out. What exactly is your use case for this? I’m really interested.

I’ve got a script I run (through pressing a dash button) which uses google:tts taking the sensor data above to read out a message to a Kodi media player telling me where people are. The JSON parsing gives me the street name and town from the results. Handy at meal times. It’s like a push-button operated audible Find My Friends. I’m quite a novice in the HA world, just hoping there’s a way around that I’ve not spotted yet.

Holy crap what a great idea!!! I can see a lot of users here that would love this.

I’m going to page @PhyberApex here and see if he can help. He’s got a much deeper understanding of templates and REST than I do and he might find the challenge interesting.

Once you get it figured out, you should post the setup in Share Your Projects.

1 Like

You are correct. First thing I tried was a command_line sensor but no luck here either. I belive this is currently not possible in the way you’d like it. I would recommend to add one more action to your mentioned script for the dash button where you call a command line script which fills those sensors for you by getting the data from the RestAPI and setting the sensors through the same. This would also reduce the GoogleAPI calls to a minumum as it only get’s called when really needed. If you need help with that I would be happy to help out.

~Cheers

1 Like

I knew you would have insight into this. I really think this could be an awesome project!

Thanks @PhyberApex. Great suggestion to use the COMMAND line sensor. I’ve proven this works, but need to build it out/test properly. I need to figure out how to limit the polling time on a sensor, possibly based on time of day, as Google has roughly one a minute API limits and I believe I need two API calls (one for street name, one for locality) per request. I guess once I understand Jinja scripting better, I will be able to extract both using value_template without recalling the command line, but can sort that later.

1 Like

Wait what…Command line worked for you? It did not for me oO

~Cheers

It worked just great. (I will write it all up, just need some time). What issue are you getting ?

1 Like

Just tried a simple

ls /home/{{ "userName"|lower }}

To see if templates work in command line sensor did not work for me :o

Mind copy-pasting your config for the command line sensor?

~Cheers

@PhyberApex - I wrote a small bit of Python that simply prints the JSON data. I’m working in Windows so cannot really help with the ls command. I’ve had to avoid using templates in sensors at all, and the way I got It working is to create one sensor for each of my 4 users, hardcoding the Python program name into the sensor. From the messing around I’ve done, I think that the sensor is “set” at initialisation only. So it takes sensor strings literally, and hence don’t think it’s ever going to work.

1 Like

Okay misunderstanding then. I thought you got templating to work with command line sensors ^^

~Cheers

Nope, sorry. Tried all sorts of connutations, got nowhere. The command line thing works fine, and will write it up.

2 Likes