Skip to content

Instantly share code, notes, and snippets.

@xbmcnut
Last active March 12, 2021 08:19
Show Gist options
  • Save xbmcnut/82df984a60a241d97c83cf8eea24a5a9 to your computer and use it in GitHub Desktop.
Save xbmcnut/82df984a60a241d97c83cf8eea24a5a9 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 1, 2 & 3
speed_template: "{{ states('sensor.template_fan_speed_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_speed:
service: script.master_fan_set_speed
data_template:
speed: "{{ speed }}"
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
speeds:
- 'Off'
- '1' ## 4.27~5.07 watts ##
- '2' ## 6.33~6.67 watts ##
- '3' ## 9.46~9.93 watts ##
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_speed:
alias: Master Fan Set Speed
sequence:
- service: input_select.select_option
data_template:
entity_id: input_select.master_fan_set_speed
option: '{{speed}}'
- 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_speed_master:
friendly_name: Master Estimated Fan Speed
value_template: >-
{% set fan_power = states('sensor.bedroom_fan_shelly_power') | float %}
{% if 3 < fan_power < 6 %}
1
{% elif 6 <= fan_power < 8 %}
2
{% elif fan_power >= 8 %}
3
{% else %}
Off
{% endif %}
## Speed Reference ##
## (Off) <3 watts
## (1) 4.27~5.07 watts
## (2) 6.33~6.67 watts
## (3) 9.46~9.93 watts
@xbmcnut
Copy link
Author

xbmcnut commented Nov 2, 2019

Updated full template to include !secret and modified speed_template and direction_template to read values from the Shelly1 PM (power/watts) and an input_boolean to fake direction feedback.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment