Something like trigger.to_state, but when using time_pattern?

Hi everybody,

is there a way to utilize something like trigger.to_state, but when using time_pattern as trigger?

Example: run this every 30 minutes:

  • entities: binary_sensor.test01, input_boolean.test02, binary_sensor.test_contact

They are all supposed to be on; but if one (ore multiple) is turned off, notify like Attention! {{ trigger.to_state.friendly_name }} should be on, but is off!.

I cannot actually use trigger.to_state.friendly_name, because the trigger is time_pattern, not any entity.

Currently, I have one automation per entity, then hardcode the notification message. But I’m sure there is a way to do this more elegantly, I just don’t know how. Can you help me solve this?

Thank you in advance for your ideas :slight_smile:

Is there a reason for using time pattern? Otherwise you could use


trigger:
  - platform: state
    entity_id: 
      - binary_sensor.a
      - binary_sensor.b
      - binary_sensor.c
    from: "on"
    to: "off"
    for:
      minutes: 30 # alternatively: "{{ states('input_number.x') |int(30) }}"


Thank you.

In this case, I need the time_pattern so that the automation will work even after restarting Home Assistant.

Let’s say I have a plant.bonsai that has an attribute moisture_status; this will change to Low when the plant needs to be watered. But I’d like a constant reminder to do so, until it has been done.

In this case, I’d check every 30 minutes whether the state is like this, and, if so, send a notification. If not, don’t do anything.

If I were to restart Home Assistant, it wouldn’t “remember” when the plant entity has reported Low, so it cannot act on for: minutes: 30.

There is what is available for time pattern:

You can have your notification like this:

{% set entities = [
  'binary_sensor.test01',
 'input_boolean.test02',
 'binary_sensor.test_contact'
  ] %}
Attention!!
The folowing entities should be OFF, but they are ON:
- {{ expand(entities) | selectattr('state', 'eq', 'on') | map(attribute='attributes.friendly_name') | list | join('
- ')}}
1 Like

Thank you! This template goes in the right direction. But for some reason, I am not able to adapt it to my scenario:

{% set entities = [
  'plant.bonsai',
  'plant.pachi',
  'plant.zwischenzimmer_pachi'
  ] %}
Attention!!
These plants need water:
- {{ expand(entities) | selectattr('moisture_status', 'eq', 'Low') | map(attribute='attributes.friendly_name') | list | join('
- ')}}

Try this:

{% set entities = [
  'plant.bonsai',
  'plant.pachi',
  'plant.zwischenzimmer_pachi'
  ] %}
Attention!!
These plants need water:
- {{ expand(entities) | selectattr('attributes.moisture_status', 'eq', 'Low') | map(attribute='attributes.friendly_name') | list | join('
- ')}}

Or this (will list any plant where moisture is not “ok”):

{% set entities = [
  'plant.bonsai',
  'plant.pachi',
  'plant.zwischenzimmer_pachi'
  ] %}
Attention!!
These plants need water:
- {{ expand(entities) | selectattr('attributes.moisture_status', 'ne', 'ok') | map(attribute='attributes.friendly_name') | list | join('
- ')}}

But maybe you should include some validation for the existence os this attribute first, otherwise it will trigger an error message:

{% set entities = [
  'plant.bonsai',
  'plant.pachi',
  'plant.zwischenzimmer_pachi'
  ] %}
Attention!!
These plants need water:
- {{ expand(entities) | selectattr('attributes.moisture_status', 'defined') |  selectattr('attributes.moisture_status', 'ne', 'ok') | map(attribute='attributes.friendly_name') | list | join('
- ')}}
1 Like

Thank you so much.

Whenever I use neq, I get this TemplateRuntimeError: No test named 'neq'.. But your other suggestion works perfectly!

I can use this to determine whether or not to send a notification at all

{% if expand(entities) | selectattr('attributes.moisture_status', 'eq', 'Low') | map(attribute='attributes.friendly_name') | list | count > 0 %}

It’s ne.

https://jinja.palletsprojects.com/en/latest/templates/#jinja-tests.ne

2 Likes

I’ve fixed on my post.
Thanks!!