Help with automation - i don't find the problem

Hi, i installed Fully Kiosk on my wallet mounted tablet and I want to activate a switch (via MQTT) when battery level is under 30% and deativate it when it is above 80%.

I created the battery sensor in configuration.yaml:

mqtt:
  sensor:
    - name: "Batteria P20HD Tablet"
      state_topic: "fully/deviceInfo/p20hd"
      unit_of_measurement: "%"
      device_class: battery
      value_template: "{{ value_json.batteryLevel }}"

It works as expected! (I show it on dashboard)
immagine

Then I’ve created first automation to enable the switch (it is a shelly) when battery level is under 30 (and on each automation scripts restart / HA start).

The problem is that the automation works only on automation restart / HA start, but not when the battery change!! For example if battery is at 11% and change to 10% (or 12%), switch not trigger to ON.

Any suggest?

automation.yaml:

- id: '1713892150631'
  alias: Controllo ricarica tablet muro (attiva ricarica)
  description: se scende sotto la soglia di 30, si attiva la ricarica tablet
  trigger:
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
  - platform: numeric_state
    entity_id:
    - sensor.batteria_p20hd_tablet
    below: 30
  condition:
  - condition: numeric_state
    entity_id: sensor.batteria_p20hd_tablet
    below: 30
  action:
  - type: turn_on
    device_id: 2922070eeed47e13885b424c45f3169d
    entity_id: switch.switch_ricarica_tablet_muro
    domain: switch
  mode: single

Your trigger requires the battery level to change from above 30 to below 30. That is how numeric state triggers work. See: https://www.home-assistant.io/docs/automation/trigger/#numeric-state-trigger

Change it to this state trigger instead:

- id: '1713892150631'
  alias: Controllo ricarica tablet muro (attiva ricarica)
  description: se scende sotto la soglia di 30, si attiva la ricarica tablet
  trigger:
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded
  - platform: state  ### "state" not "numeric_state"
    entity_id:
    - sensor.batteria_p20hd_tablet
    not_to:  # triggers on all numbers
      - unknown
      - unavailable
  condition:
  - condition: numeric_state
    entity_id: sensor.batteria_p20hd_tablet
    below: 30
  action:
  - type: turn_on
    device_id: 2922070eeed47e13885b424c45f3169d
    entity_id: switch.switch_ricarica_tablet_muro
    domain: switch
  mode: single

This will trigger on all battery level changes (except unknown or unavailable). Then check the condition to see if the battery level is below 30.

1 Like

Thank you! It works!
All tutorial I’ve found about battery control, are bad! Always they use “numeric_state”

They are probably not expecting the battery to already be below the trigger level.

Your automation would work. It’s just that the level is already below 30. You would have to force it to change to above 30 then next time it falls below 30 your automation would trigger.

The way I have it triggers a lot more often but will catch this edge case.

1 Like

Ok, so to prevent useless triggers, can i just add a condition like this? (I’ve found on community, i don’t know if there is a better way to find “on” or “off” state of switch)

 condition:
  - condition: numeric_state
    entity_id: sensor.batteria_p20hd_tablet
    below: 30
  - condition: template
    value_template: "{{ states.switch.switch_ricarica_tablet_muro|selectattr('state','equalto','off')|list|length > 0 }}

No, conditions won’t prevent triggers from occuring. They only prevent the actions from running.

This will trigger every time the sensor changes:

  - platform: state  ### "state" not "numeric_state"
    entity_id:
    - sensor.batteria_p20hd_tablet
    not_to:  # triggers on all numbers
      - unknown
      - unavailable

But this will prevent the action occurring if above 30:

  condition:
  - condition: numeric_state
    entity_id: sensor.batteria_p20hd_tablet
    below: 30

Where as this will only trigger when the battery level crosses from above to below 30

  - platform: numeric_state
    entity_id:
    - sensor.batteria_p20hd_tablet
    below: 30
  - platform: homeassistant
    event: start
  - platform: event
    event_type: automation_reloaded

However if the battery level is already below 30 when you create the automation you will need to restart or reload to get it to fire the first time.

1 Like