Automation Trigger - battery_level for a device

I want to have the below: attribute to vary with the month ie replace the 40 with a numeric variable

type: battery_level
platform: device
device_id: 72d9d816134e08e58d2abe62fe2d6768
entity_id: sensor.givtcp_ce2142g314_soc
domain: sensor
below: 40
for:
  hours: 0
  minutes: 15
  seconds: 0

My attempt which doesn’t work

type: battery_level
platform: device
device_id: 72d9d816134e08e58d2abe62fe2d6768
entity_id: sensor.givtcp_ce2142g314_soc
domain: sensor
below: "{{ [100,100,80,80,60,40,0,0,10,60,100,100][now().month - 1] |float(0)}}"
for:
  hours: 0
  minutes: 15
  seconds: 0
  1. You’re using a Device Trigger which doesn’t support templates for any of its options (neither does a Device Condition or a Device Action).

  2. Even if you were to use a Numeric State Trigger, which does support templates, it doesn’t support templates for its below option.

What a Numeric State Trigger does support for its below option is a direct reference to a sensor entity. Therefore I suggest you create a Template Sensor and then reference it in a Numeric State Trigger.

Example: Template Sensor

template:
  - sensor:
      - name: Battery Threshold
        state: "{{ [100,100,80,80,60,40,0,0,10,60,100,100][now().month - 1] }}"

Example: Numeric State Trigger

trigger:
  - platform: numeric_state
    entity_id: sensor.givtcp_ce2142g314_soc
    below: sensor.battery_threshold
    for:
      minutes: 15

NOTE

The Battery Threshold sensor only needs to update its template at the start of every day. The example posted above will update the template every minute which is far more than necessary. The following is an example of Trigger-based Template Sensor and will update just once a day and can be used instead of the Template Sensor example.

Example: Trigger-based Template Sensor

template:
  - trigger:
      - platform: time
        at: '00:00:01'
    sensor:
      - name: Battery Threshold
        state: "{{ [100,100,80,80,60,40,0,0,10,60,100,100][now().month - 1] }}"

Many, many thanks. Works perfectly.

1 Like

You’re welcome!

Please consider marking my post above with the Solution tag. It will automatically place a check-mark next to the topic’s title which signals to other users that this topic has been resolved. This helps users find answers to similar questions.

For more information about the Solution tag, refer to guideline 21 in the FAQ.