I have script on a device that updates its IP address into a sensor within HA. The script looks like this (truncated to make sense):
now = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
ip_url = 'https://api64.ipify.org?format=json'
...
response = urllib2.urlopen(ip_url,timeout=5)
data = json.loads(response.read())
updt_txt = data["ip"]
...
ha_payload = '{"state":"'+updt_txt+'","attributes":{"last_triggered":"'+now+'"}}'
The sensor then looks like this in HA:
I then have a sensor that evaluates the IP:
{{ not ( is_state('sensor.ip', 'unknown') or is_state('sensor.ip', 'TIMEOUT') or is_state('sensor.ip', 'URL_ERROR') ) }}
I want to extend this sensor to also evaluate if the ip has not been updated within 10 minutes. I thought I could evaluate the attribute by using this:
as_timestamp(utcnow()) - as_timestamp(state_attr('sensor.ip','last_triggered'))
But that is giving me the UTC time conversion of the last_triggered
item, which is already UTC converted. Is there a function that I should be using instead of as_timestamp
? Thanks!