Help with automation template & value_template

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.

Please format your code to make it more legible for debugging,

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
    value_template: "{{ states('sensor.netatmo_outdoor_battery_percent') | int < 95 }}"
1 Like

Legend thanks heaps - solved automation fired perfectly :slight_smile:

No worries. Do you see why?

I started the home automation adventure in Nov18 I’m learning daily, yes I see why…now.

1 Like

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.

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.

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”! :slight_smile:

2 Likes