Vacuum attribute template not updating

Hello everyone,

Recently I purchased a dumb IR controlled vacuum robot and I’ve been controlling it through a Broadlink device. I went ahead and added it as a template:

vacuum:
  - platform: template
    vacuums:
      wap300:
        friendly_name: Robô Aspirador WAP300
        value_template: "{{ states('input_select.wap_300_state') }}"
        fan_speed_template: "{{ states('input_select.wap_300_fan_speed') }}"
        start:
           service: script.wap300_start
        pause:
           service: script.wap300_pause
        return_to_base:
           service: script.wap300_return_to_base
        set_fan_speed:
           service: script.wap300_set_fan_speed
        fan_speeds:
            - Normal
            - Super
        attribute_templates:
          last_clean: "{{ states('input_datetime.wap_300_last_clean') }}"
          next_clean: "{{ (as_timestamp(now()) - as_timestamp(states('input_datetime.wap_300_last_clean'))) > (20*3600) }}"
          next_clean_home: "{{ (as_timestamp(now()) - as_timestamp(states('input_datetime.wap_300_last_clean'))) > (48*3600) }}"

I used some helpers to control state, fan speed and keep track of when was the last clean. When script.wap300_start is called, I update the input_datetime.wap_300_last_clean to current time.

The goal is: If last clean was 20 hours ago and house is empty, start. If last clean was 48 hours ago, start as long as the alarm is disarmed. Here’s one of the automations:

- id: '1601648927245'
  alias: Vacuum - Start away cleaning
  description: ''
  trigger:
  - platform: state
    entity_id: vacuum.wap300
    attribute: next_clean
    to: 'True'
  - platform: state
    entity_id: alarm_control_panel.ha_alarm
    to: armed_away
  condition:
  - condition: state
    entity_id: alarm_control_panel.ha_alarm
    state: armed_away
  - condition: state
    entity_id: vacuum.wap300
    state: 'True'
    attribute: next_clean
  action:
  - service: vacuum.start
    data: {}
    entity_id: vacuum.wap300
  - delay: '5'
  - service: vacuum.set_fan_speed
    data: {}
    entity_id: vacuum.wap300
  mode: single

My problem: These two time based templates below stay as “False” even if it has been 3 days since last clean, if I copy the template and check in “Developer Tools” they end up returning “True”, and they also return “True” if I restart HA but it never works by itself. It seems to only update on restart and if I manually change the input.

          next_clean: "{{ (as_timestamp(now()) - as_timestamp(states('input_datetime.wap_300_last_clean'))) > (20*3600) }}"
          next_clean_home: "{{ (as_timestamp(now()) - as_timestamp(states('input_datetime.wap_300_last_clean'))) > (48*3600) }}"

Is my template wrong or should I try another method here?

Thanks in advance.

Assuming you have the Time & Date integration installed with sensor.time defined, you can do this:

next_clean: "{{ (states.sensor.time.last_updated.timestamp() - as_timestamp(states('input_datetime.wap_300_last_clean'))) > (20*3600) }}"
 
next_clean_home: "{{ (states.sensor.time.last_updated.timestamp() - as_timestamp(states('input_datetime.wap_300_last_clean'))) > (48*3600) }}"
1 Like

The templates don’t update because you don’t have a sensor attached to them to invoke updates.

Integrate sensor.time, and add this line to your templates:

          next_clean: >
            {% set dump = states('sensor.time') %}
            {{ (as_timestamp(now()) - as_timestamp(states('input_datetime.wap_300_last_clean'))) > (20*3600) }}
          next_clean_home: >
            {% set dump = states('sensor.time') %}
            {{ (as_timestamp(now()) - as_timestamp(states('input_datetime.wap_300_last_clean'))) > (48*3600) }}

This will update them once a minute.

1 Like

Thanks for the fast replies and also for explaining why it didn’t update!

I’ve updated the templates and will also pay attention to this in the future, thanks again.

1 Like