Turn on led strip, smartphone battery below 20%

Goodnight. I wanted to create an automation for my led tape to turn on when the cell phone battery was below 20%. I’m not able to turn it off when the battery is above 20%. Someone can help me?

alias: aa
description: ''
trigger:
  - platform: numeric_state
    entity_id: sensor.iphone_11_tr_battery_level
    above: '0'
    below: '20'
condition: []
action:
  - service: light.turn_on
    target:
      entity_id: light.fita_led
mode: single


trigger:
  - platform: numeric_state
    entity_id: sensor.iphone_11_tr_battery_level
    below: '20'
  - platform: numeric_state
    entity_id: sensor.iphone_11_tr_battery_level
    above: '20'
action:
  - service: light.turn_{{ 'on' if states('sensor.iphone_11_tr_battery_level') | float < 20.0 else 'off' }}
    target:
      entity_id: light.fita_led
mode: single

Stick an id of ‘on’ and ‘off’ in the triggers. Then in service you can just use
light.turn_{{ trigger.id }} without the need for the if stuff.

2 Likes

Yeah, I keep forgetting about the new trigger id stuff. That would be cleaner but either way will work.

:+1:

1 Like

I personally use choose blocks with trigger id’s. Waaaay more verbose but still easy peazy with the UI and I suspect more future-proof/less likely to break later.

in this case, could you tell me how it would look?

I would make it “above: 20 and below: 100”, and “above: 0 and below: 20”.
The way it is now it will send commands to the LED strip every time the battery level changes.

nope.

it will only trigger the automation one time as the battery level drops below 20 and then only once when it goes back above 20 again.

the numeric state trigger only becomes true when it crosses the threshold. not continuously when it is below that threshold.

Something is wrong with the action… :s

really?

I’m pretty sure it’s ok but just in case try this instead:

action:
  - service: >
      light.turn_{{ 'on' if states('sensor.iphone_11_tr_battery_level') | float < 20.0 else 'off' }}
    target:
      entity_id: light.fita_led

also you can check the logs or automation trace to try to see why the automation is failing.

as a matter of fact I just created a test automation and tested it and verified it worked:

  - alias: test battery automation
    trigger:
      - platform: numeric_state
        entity_id: input_number.test_input_number
        below: '20'
      - platform: numeric_state
        entity_id: input_number.test_input_number
        above: '20'
    action:
      - service: light.turn_{{ 'on' if states('input_number.test_input_number') | float < 20.0 else 'off' }}
        target:
          entity_id: light.comp_corner_lamp_1df9
    mode: single

if I set the input number to 19 the light goes on and if I set it to 21 the light goes off.

So it has to be some other reason that the action syntax being wrong.

it’s great! Thanks!

Good afternoon. just one more question. if I want to put more devices with the same rule, in the same automation, how is it?

asuming you want to turn on and off the same light:

trigger:
  - platform: numeric_state
    entity_id: 
      - sensor.iphone_11_tr_battery_level
      - sensor.iphone_some_other_battery_level
      - sensor.iphone_and_another_battery_level
    below: '20'
  - platform: numeric_state
    entity_id: 
      - sensor.iphone_11_tr_battery_level
      - sensor.iphone_some_other_battery_level
      - sensor.iphone_and_another_battery_level
    above: '20'
action:
  - service: >
      light.turn_{{ 'on' if trigger.to_state.state | float < 20.0 else 'off' }}
    target:
      entity_id: light.fita_led
mode: single

hi… The light does not turn off when the battery goes above 20%. Do you know what it might be?

Not really.

Have you looked at the automation trace to see why it isn’t working?