Skip to content

Instantly share code, notes, and snippets.

@bdraco
Forked from xbmcnut/shellypm1_fan.yaml
Last active March 12, 2021 08:25
Show Gist options
  • Save bdraco/fa1d51f6ff85ce1ecc5cc9c5b7ca0314 to your computer and use it in GitHub Desktop.
Save bdraco/fa1d51f6ff85ce1ecc5cc9c5b7ca0314 to your computer and use it in GitHub Desktop.
How to make a dumb device smart using a Shelly PM1 | Fan Template example in Home Assistant
fan:
- platform: template
fans:
template_bedroom_fan:
friendly_name: "Master Bedroom Fan"
# Measures the power use from a Shelly1 PM and if over 3W, marks the fan as on
value_template: "{% if states('sensor.bedroom_fan_shelly_power') | float > 3 %}on{% else %}off{% endif %}"
# Uses the Shelly1 PM sensor value to determine what speed the fan is going at. Only set to measure 33, 66, 100
percentage_template: "{{ states('sensor.template_fan_percentage_master') }}"
# Uses a fake switch from an input_boolean as the fan has no way of reporting direction back to HA
direction_template: "{{ 'forward' if is_state('input_boolean.master_fan_direction', 'on') else 'reverse' }}"
turn_on:
service: script.bedroom_fan_on
turn_off:
service: script.bedroom_fan_off
set_percentage:
service: script.master_fan_set_percentage
data_template:
percentage: "{{ percentage }}"
set_direction:
- service: broadlink.send
data:
host: !secret broadlink_ip
packet: !secret 0101_direction
- service: input_boolean.toggle
entity_id: input_boolean.master_fan_direction
speed_count: 3
input_select:
master_fan_set_speed:
name: Master Bedroom Speed
options:
- 'Off'
- '1'
- '2'
- '3'
icon: mdi:fan
## Creates a fake sensor to allow the fwd/rev template to work ##
input_boolean:
master_fan_direction:
name: Master Fan Direction
## SCRIPTS ##
script:
bedroom_fan_off:
alias: MasterFan (Off)
sequence:
service: broadlink.send
data:
host: !secret broadlink_ip
packet: !secret 0101_stop
bedroom_fan_on:
alias: MasterFan (On)
sequence:
- service_template: >
{% if is_state("input_select.master_fan_set_speed", "Off") %}
script.bedroom_fan_off
{% elif is_state("input_select.master_fan_set_speed", "1") %}
script.bedroom_fan_1
{% elif is_state("input_select.master_fan_set_speed", "2") %}
script.bedroom_fan_2
{% elif is_state("input_select.master_fan_set_speed", "3") %}
script.bedroom_fan_3
{% endif %}
bedroom_fan_1:
alias: Master Fan Speed 1
sequence:
- service: broadlink.send
data:
host: !secret broadlink_ip
packet: !secret 0101_speed1
bedroom_fan_2:
alias: Master Fan Speed 2
sequence:
- service: broadlink.send
data:
host: !secret broadlink_ip
packet: !secret 0101_speed2
bedroom_fan_3:
alias: Master Fan Speed 3
sequence:
- service: broadlink.send
data:
host: !secret broadlink_ip
packet: !secret 0101_speed3
## Forum feedback that suggested this code here ##
## https://community.home-assistant.io/t/template-fan-configuration/60603/9 ##
master_fan_set_percentage:
alias: Master Fan Set Speed
sequence:
- service: input_select.select_option
data_template:
entity_id: input_select.master_fan_set_speed
option: >
{% if percentage > 66 %}
3
{% elif percentage > 33 %}
2
{% elif percentage > 0 %}
1
{% else %}
0
{% endif %}
- service: script.turn_on
data_template:
entity_id: script.bedroom_fan_{{ speed }}
## Forum feedback that suggested this code here ##
## https://community.home-assistant.io/t/help-with-a-template-sensor-that-sets-a-number-based-on-values-between-two-numbers/131888/3 ##
## Sensor is used for the speed_template to determine how fast the fan is going based on power consumption
sensor:
- platform: template
sensors:
template_fan_percentage_master:
friendly_name: Master Estimated Fan Percentage
value_template: >-
{% set fan_power = states('sensor.bedroom_fan_shelly_power') | float %}
{% if 3 < fan_power < 6 %}
33
{% elif 6 <= fan_power < 8 %}
66
{% elif fan_power >= 8 %}
100
{% else %}
Off
{% endif %}
## Speed Reference ##
## (Off) <3 watts
## (33) 4.27~5.07 watts
## (66) 6.33~6.67 watts
## (100) 9.46~9.93 watts
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment