Problem with automation based on template trigger

I am using HA OS

  • Core 2024.10.3
  • Supervisor 2024.10.2
  • Operating System 13.2
  • Frontend 20241002.3

Hi, I want to make an automation to switch a power supply on or off based on the power level of my ipad
I tried first to use the entity directly in a numerical trigger, but it did now work. I then saw in a post that the value returned by the entity is a string, and I therefor needed to use a template trigger. I then used the example but it does not seem to work.
I made the automation with the UI, but want to join the generated yaml

alias: IpadKioskPowerControl
description: ""
triggers:
  - trigger: template
    id: fullycharged
    value_template: "{{states.sensor.marc_s_ipad_mini_battery_levelstate | int>90}}"
  - trigger: template
    id: lowpower
    value_template: "{{states.sensor.marc_s_ipad_mini_battery_levelstate | int<10}}"
conditions: []
actions:
  - choose:
      - conditions:
          - condition: trigger
            id:
              - fullycharged
        sequence:
          - action: switch.turn_off
            metadata: {}
            data: {}
            target:
              device_id: 1c8ce02144c5291d40efb9b4a9d68120
      - conditions:
          - condition: trigger
            id:
              - lowpower
        sequence:
          - action: switch.turn_on
            metadata: {}
            data: {}
            target:
              device_id: 1c8ce02144c5291d40efb9b4a9d68120
mode: single

every help would be much appreciated

alias: IpadKioskPowerControl
description: ""
trigger:
  - platform: template
    value_template: "{{ states.sensor.marc_s_ipad_mini_battery_level.state | int > 90 }}"
    id: fullycharged
  - platform: template
    value_template: "{{ states.sensor.marc_s_ipad_mini_battery_level.state | int < 10 }}"
    id: lowpower
condition: []
action:
  - choose:
      - conditions:
          - condition: trigger
            id: fullycharged
        sequence:
          - service: switch.turn_off
            target:
              device_id: 1c8ce02144c5291d40efb9b4a9d68120
      - conditions:
          - condition: trigger
            id: lowpower
        sequence:
          - service: switch.turn_on
            target:
              device_id: 1c8ce02144c5291d40efb9b4a9d68120
mode: single

There’s no need for template triggers. Use the numeric state trigger.

But if you did use it, then the best format is:

value_template: "{{ states('sensor.marc_s_ipad_mini_battery_level') | int > 90 }}"

Hi Marc,

Looking at your underlying concern, I want to make sure that you know that the number being monitored has to pass thru the threshold in order to trigger. Triggers are not going to trigger because the number is above or below a value, it will trigger as it passes thru a level or trigger point.

There are different ways using the conditions statement to catch those conditions.

Automations #1: trigger only fires when it changes from not true to true.