qwackers
(Simon)
January 20, 2019, 6:52am
1
Ok folks HELP…
I’m doing my head in, I’ve googled back & forth, upside down and inside out. I simply can’t nut out why my automation throws error after error.
Here’s the code that I just can’t get the value_template to work I plugged the .
- alias: battery check
trigger:
- at: '10:00'
platform: time
condition:
- condition: state
entity_id: 'device_tracker.xxxxxxxxxxxxx'
state: 'home'
- condition: time
weekday:
- sun
- condition: template
value_template: "{{ states.sensor.netatmo_outdoor_battery_percent.state < 95 }}"
action:
- data:
data:
priority: 1
sound: bugle
device: iPhone
message: 'Netatmo Outdoor Battery = {{ states.sensor.netatmo_outdoor_battery_percent.state }}%'
title: Home Assistant
service: notify.pushover
I’ve used the template developer tool and {{ states.sensor.netatmo_outdoor_battery_percent.state }} returns 78.
If I remove the template condition everything works fine.
Thanks in advance.
123
(Taras)
January 20, 2019, 7:35am
2
Please format your code to make it more legible for debugging,
qwackers
(Simon)
January 20, 2019, 8:30am
3
Oops I didn’t notice the reformatting. Lets try that again.
- alias: netatmo battery check
trigger:
- at: '17:35'
platform: time
condition:
- condition: state
entity_id: 'device_tracker.xxxxxxxxxxxxx'
state: 'home'
- condition: time
weekday:
- sun
- condition: template
value_template: "{{ states.sensor.netatmo_outdoor_battery_percent.state < 95 }}"
action:
- data:
data:
priority: 1
sound: bugle
device: iPhone
message: 'Netatmo Outdoor Battery = {{ states.sensor.netatmo_outdoor_battery_percent.state }}%'
title: Home Assistant
service: notify.pushover
tom_l
January 20, 2019, 9:07am
4
value_template: "{{ states('sensor.netatmo_outdoor_battery_percent') | int < 95 }}"
1 Like
qwackers
(Simon)
January 20, 2019, 9:30am
5
Legend thanks heaps - solved automation fired perfectly
tom_l
January 20, 2019, 12:08pm
6
No worries. Do you see why?
qwackers
(Simon)
January 20, 2019, 8:53pm
7
I started the home automation adventure in Nov18 I’m learning daily, yes I see why…now.
1 Like
123
(Taras)
January 20, 2019, 9:22pm
8
qwackers:
yes I see why…now
To help other newcomers who may read this thread, can you take a moment to explain what you learned about Jinja2 that required the use of int
.
qwackers
(Simon)
January 20, 2019, 11:02pm
9
Honestly I still don’t understand it completely, I thought the state was an INT already. I really have a lot to learn about Jinja2.
123
(Taras)
January 20, 2019, 11:27pm
10
No worries, these things aren’t always obvious. You’re right to assume the value is an integer given that it is a sensor handling battery level and displayed as a number. However, by default, everything is handled as a string value not a numeric value. If you wish to do arithmetic or compare quantities, you have to convert the string value to a numeric value using either int
(integer; whole numbers) or float
(floating point; numbers with fractional values). This of course assumes the string you wish to convert contains numeric characters, like “48”, and not something like “cat”!
2 Likes