Trigger templating

I have several Roborock vacuums and have one automation for all vacuums to provide a notification when complete. I took advantage of some of the dynamic attributes to do so.

Since some of the attributes have been changed to sensors, I am at a loss to get up and running again. How can I dynamically use the value_template to evaluate a sensor based on the vacuum being docked since I used to use the attributes?

The names of the sensors I could use are (and repeat for the other two vacuums respectively)
sensor.one_last_clean_start
sensor.one_last_clean_end
sensor.one_last_clean_duration

- alias: Vacuum - Completed
  trigger:
    - platform: state
      entity_id:
        - vacuum.one
        - vacuum.two
        - vacuum.three
      from: 'returning'
      to: 'docked'
  condition:
    - condition: template
      value_template: "{{ as_timestamp(trigger.to_state.attributes.clean_stop)-as_timestamp(trigger.to_state.attributes.clean_start) < 25000}}"

Post examples of the values of sensor.one_last_clean_start and sensor.one_last_clean_end.

sensor.one_last_clean_start: 2021-11-05T08:00:01
sensor.one_last_clean_end: 2021-11-05T09:24:56
sensor.one_last_clean_duration: 5095

Try this

  condition:
    - condition: template
      value_template: >
        {% set start = 'sensor.{}_last_clean_start'.format(trigger.to_state.object_id) | as_timestamp %}
        {% set end = 'sensor.{}_last_clean_end'.format(trigger.to_state.object_id) | as_timestamp %}
        {{ end - start < 25000 }}

Thanks. I received this error:

Error evaluating condition in ‘Vacuum - Completed’: In ‘condition’: In ‘template’ condition: TypeError: unsupported operand type(s) for -: ‘NoneType’ and ‘NoneType’

  1. How did you test it?
  2. Post the code of the entire automation you tested.
- alias: Vacuum - Completed
  trigger:
    - platform: state
      entity_id:
        - vacuum.one
        - vacuum.two
        - vacuum.three
      from: 'returning'
      to: 'docked'
  condition:
    - condition: template
      value_template: >
        {% set start = 'sensor.{}_last_clean_start'.format(trigger.to_state.object_id) | as_timestamp %}
        {% set end = 'sensor.{}_last_clean_end'.format(trigger.to_state.object_id) | as_timestamp %}
        {{ end - start < 25000 }}
#    - condition: template
#      value_template: "{{ as_timestamp(trigger.to_state.attributes.clean_stop)-as_timestamp(trigger.to_state.attributes.clean_start) < 25000}}"
#    - condition: template
#      value_template: "{{ as_timestamp(trigger.to_state.attributes.clean_stop) | timestamp_custom('%Y-%m-%d') == states('sensor.date') }}"
  action:
    - service: notify.bullet
      data_template:
        title: "Vacuum Complete"
        message: "{% if trigger.entity_id == 'vacuum.one' and now().isoweekday() == 3 and as_timestamp(now()) | timestamp_custom('%H') < '07'  %}
                   Bedroom Vacuum Completed
                   {% elif trigger.entity_id == 'vacuum.one' and now().isoweekday() == 1 and as_timestamp(now()) | timestamp_custom('%H') < '07'  %}
                   Dining Room Vacuum Completed
                   {% else %}
                   {{ state_attr(trigger.entity_id, 'friendly_name') }} Vacuum Completed
                   {% endif %}"

Did you test it by making one of the three vacuums change state from returning to docked?

That is how I tested it.

OK. I see my mistake now. I forgot to include the states() function! :man_facepalming:

Try this revised version.

  condition:
    - condition: template
      value_template: >
        {% set start = states('sensor.{}_last_clean_start'.format(trigger.to_state.object_id)) | as_timestamp %}
        {% set end = states('sensor.{}_last_clean_end'.format(trigger.to_state.object_id)) | as_timestamp %}
        {{ end - start < 25000 }}

That did the trick! Thank you very much!!!

1 Like