Use multiple sensors in one automation to avoid duplication

I’m using the whois platform to get the expiration dates of my domains

- platform: whois
  domain: domain1.com
  name: domain1_sensor
- platform: whois
  domain: domain2.com
  name: domain1_sensor

For each domain, I have an automation to send a notification when there are fewer than 31 days to expiry.

- alias: domain1_sensor_notify
  trigger:
    platform: time
    at: "10:00"
  condition:
    condition: numeric_state
    entity_id: sensor.domain1
    below: 31
  action:
    service: notify.email
    data_template:
      title: "domain1.com expiring"
      message: "Remaining days {{states.sensor.domain1_sensor.state}}"

- alias: domain2_sensor_notify
  trigger:
    platform: time
    at: "10:00"
  condition:
    condition: numeric_state
    entity_id: sensor.domain2
    below: 31
  action:
    service: notify.email
    data_template:
      title: "domain2.com expiring"
      message: "Remaining days {{states.sensor.domain1_sensor.state}}"

I’d like to have a single automation that works for all whois sensors. I’m new to writing automations, so could anyone suggest how to do this?

I believe you can add entity_id

entity_id:
  - sensor.domain1
  - sensor.domain2

You could just trigger off the state of either sensor going below 31:

- alias: domain_sensor_notify
  trigger:
    platform: numeric_state
    entity_id:
      - sensor.domain1
      - sensor.domain2
    below: 31
  action:
    service: notify.email
    data_template:
      title: "domain expiring"
      message: "{{ trigger.to_state.friendly_name )): remaining days {{trigger.to_state.state}}"

Without checking the details I think that should work.

This:

trigger.to_state.attributes.friendly_name

or this:

trigger.to_state.name

Though if you wish to retain the 10am trigger for the message, create a group containing all your domain sensors. Lets call it group.my_domains

- alias: domain_sensor_notify
  trigger:
    platform: time
    at: "10:00"
  condition:
    condition: template
    value_template: >
      {{ states|selectattr('entity_id','in',state_attr('group.my_domains','entity_id'))|selectattr('state','lessthan', 31 )|list|count|int > 0 }}
  action:
    service: notify.email
    data_template:
      title: "The following domains are expiring"
      message: >
        {{ states|selectattr('entity_id','in',state_attr('group.my_domains','name'))|selectattr('state','lessthan', 31 )|list|join(', ')  }}

Though this does not give the number of days left for each sensor. I’m still thinking about how to do that.

1 Like

Thanks I’ll give this a go.

When I try to trigger it manually through the web interface, I see the error

Undefined Error: 'trigger' is undefined

I’m not sure if that’s because there’s a problem with my config, or if I just have to wait until it triggers for real.

I’d go the dynamic messaging route, but it would be a longer message:

- alias: Message Me About Domains
  trigger:
  - platform: time
    at: "10:00"
  condition:
  - condition: template
    value_template: >
      {%- set ns = namespace(cnt=0) %}
      {%- for s in states.sensor | selectattr('attributes.expires', 'defined') %}
      {%- if s.state | int < 31 %}
      {%- set ns.cnt = ns.cnt + 1 %}
      {%- endif %}
      {%- endfor %}
      {{ ns.cnt > 0 }}
  action:
  - service: notify.email
    data_template:
      title: Expiring Domains
      message: >
        The following domains are set to expire:
        {%- for entity in states.sensor | selectattr('attributes.expires', 'defined') %}
          {%- if entity.state | int < 31 %}
            {{ entity.name }} will expire in {{ entity.state }} days.
          {%- endif %}
        {%- endfor %}

EDIT: I had to fix a ton of typos, long morning. If it doesn’t work out of the box, take a screenshot of the entities and I can fix the generator.

1 Like

Thanks for the suggestion. The value_template is a bit more complicated to understand as a home-assistant beginner, but keeping the 10am trigger makes it easier to test by temporarily setting it 2 minutes in the future.

Petro’s solution actually lists the expiring domains as well as the number of days left. Mine just lists the expiring domains. Go with his solution.

Edit: hey @petro in your template, should this:

selectattr('attributes.expires', 'defined')

be:

selectattr('attributes.expiration_date', 'defined')

Edit 2: never mind. Had a look at the source and your attribute is correct. The documentation is misleading.

Yeah, I went straight to the code because there’s no ‘good’ resource for possible attributes.

Thanks for the suggestion. It gives the error:

TypeError: '<' not supported between instances of 'str' and 'int'.

How can we convert to an int before the < 31 comparison?

oof, forgot about that. Hmm. It’s going to be a harder template for the condition

      {%- set ns = namespace(cnt=0) %}
      {%- for s in states.sensor | selectattr('attributes.expires', 'defined') %}
      {%- if s.state | int < 31 %}
      {%- set ns.cnt = ns.cnt + 1 %}
      {%- endif %}
      {%- endfor %}
      {{ ns.cnt > 0 }}

EDIT: I edited the previous comment for a full solution.

EDIT EDIT: Gotta change the other template too… hold up.

That’s working now. Thanks everybody for your help and suggestions.