Skip to content

Instantly share code, notes, and snippets.

@jcallaghan
Last active October 27, 2021 07:59
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jcallaghan/ccf295af47f841646b738d1a7eff3ba8 to your computer and use it in GitHub Desktop.
Save jcallaghan/ccf295af47f841646b738d1a7eff3ba8 to your computer and use it in GitHub Desktop.
This Home Assistant automation is trigger through an Alexa automation.
alias: Alexa - It's stinky
trigger:
# Trigger based on Alexa routine turning on the light.
- platform: state
entity_id: light.alexa_virtual
to: 'on'
condition:
# Check the brightness was set to 21. Why 21? To avoid clashes 20 in the range for my bathroom.
# Brightness can only be set between 1 and 100 in a Alexa routine so we perform a calculation so the number range matches.
- condition: template
value_template: "{{ (state_attr('light.alexa_virtual','brightness') | int / 255 * 100) | round == 21 }}"
action:
# Refresh the last Alexa called
- service: alexa_media.update_last_called
# The delay allows the sensor to refresh
- delay: 00:00:01
# Turn off the virtual light
- service: light.turn_off
entity_id: light.alexa_virtual
# Three blasts of the air freshener should be enough to remove the stink. The delays allow the switch to operate.
- service: switch.turn_on
data_template:
# By removing words from alexa_media.update_last_called sensor I can determine the room and then can create the entity_id of the air freshener in that room.
entity_id: "{{ 'switch.esph_' + states('sensor.alexa_last_used') | replace('media_player.alexa_','') | replace('esph_','') | replace('_alexa','') | replace('_sonos','') + '_air_freshener' }}"
- delay: 00:00:01
- service: switch.turn_on
data_template:
entity_id: "{{ 'switch.esph_' + states('sensor.alexa_last_used') | replace('media_player.alexa_','') | replace('esph_','') | replace('_alexa','') | replace('_sonos','') + '_air_freshener' }}"
- delay: 00:00:01
- service: switch.turn_on
data_template:
entity_id: "{{ 'switch.esph_' + states('sensor.alexa_last_used') | replace('media_player.alexa_','') | replace('esph_','') | replace('_alexa','') | replace('_sonos','') + '_air_freshener' }}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment