I want to increase fan speed of bathroom if someone has been there for at least 15 minutes.
I’m trying to set up an automation using the light off as trigger and a condition.
Unfortuntely, there is no “previous_state” timestamp, while “last_changed” does not help because light has been just turned off.
I don’t want to use external resources, like new historic sensors or helpers, because I want to create several similar automation and don’t want to complicate it too much.
Any idea?
(already tried the search and chatgpt)
triggers:
- trigger: state
entity_id: light.bathroom_light # change this
to: 'on'
for:
minutes: 15
- trigger: state
entity_id: light.bathroom_light # change this
to: 'off'
actions:
- action: "switch.turn_{{ trigger.to_state.state }}"
target:
entity_id: switch.bathroom_fan # change this
Another option would be to have an input_boolean flag that is turned on when the light has been on for 15 minutes, then use that as a condition in an automation triggered by the light turning off.
Or… Trigger when the light has been on for 15 minutes, then wait for trigger with the light going off.
The first trigger from Jack’s reply will do the job (unless you really can only increase the fan speed when the lights are turned off). You can add more light entities to it:
- trigger: state
entity_id:
- light.bathroom_light
- light.other_light
to: 'on'
for:
minutes: 15
and then have a variable with mapping light → fan, and use it in fan action.
Or you can go ballistic with trigger-based sensor It has ‘helpers’ built-in and increases the fan speed when the lights are turned off. I created it as exercise
Code
- trigger:
# trigger to immediataly see this sensor's state/attribute after template reload
- trigger: event
event_type: event_template_reloaded
id: 'reload'
# trigger to set a 'helper' for a light to true
- trigger: state
entity_id:
# you can add more lights
- light.bathroom_light
to: 'on'
for:
minutes: 15
id: light_on
# trigger to conditionally turn on fan and set a 'helper' for a light to false
- trigger: state
entity_id:
# you can add more lights
- light.bathroom_light
to: 'off'
id: light_off
action:
- variables:
# variable returns entity_id of a light for which a fan should increase speed, or None
# it reads the attribute of this very sensor (unfortunately, you can't use 'this.attributes' here)
light_entity_id: >-
{% if trigger.id == 'light_off' %}
{% set helpers = state_attr('sensor.speed_up_fan', 'helpers') or {} %}
{{ trigger.entity_id if helpers.get(trigger.entity_id,False) else None }}
{% else %}
None
{% endif %}
- if: "{{ light_entity_id is not none }}"
then:
- variables:
# here you have to create a static mapping for each light entity --> fan entity
light_to_fan:
light.bathroom_light: fan.bathroom_fan
light.other_light: fan.other_fan
- action: fan.increase_speed
target:
entity_id: "{{ light_to_fan[light_entity_id] }}"
continue_on_error: true
sensor:
- name: Speed up fan
unique_id: 3e8642f0-65af-40b3-a979-61b94d904c74
state: Automation to increase fan speed when light goes off
attributes:
# 'automatic' dictionary to keep a boolean 'helper' for each light entity:
# light.bathroom_light: true/false
# light.other_light: true/false
helpers: >-
{% set current = this.attributes.get('helpers',{}) %}
{% if trigger.id in ['light_on','light_off'] %}
{% set item = {trigger.entity_id: (trigger.id == 'light_on')} %}
{{ dict(current, **item) }}
{% else %}
{{ current }}
{% endif %}
There is… the trigger variable holds both the to_state of the triggering entity as well as the from_state.
triggers:
- trigger: state
entity_id: light.bathroom_light
to: 'off'
from: 'on'
conditions:
- alias: The light was in its previous state ('on') for at least 15 minutes
condition: template
value_template: |
{{ now() - trigger.from_state.last_changed >= timedelta(minutes=15)}}
actions:
#YOUR FAN ACTIONS