I wasn’t entirely sure where to post this, but decided rather than revive a 2 year old thread I’ll post here as it’s still more active. I have a challenge trying to adapt the code from Help with a template sensor that sets a number based on values between two numbers and integrate this information into the fan component.
My fans have lights built-in, running off the same DC driver with RF control. In my existing setup, the states are simply set by the last RF command sent, which for various reasons often ends up out-of-sync.
I couldn’t find a suitable replacement DC controller therefore am testing out the use of a Shelly 1PM on the AC supply side of the DC controller to monitor energy consumption and feed this information back into HA.
So I needed to take a set of combined energy consumption values and get HA to validate the state of both the fan and the light. Below are the stable values I observed in the power monitoring:
Fan & light both off: 0.8 - 1.0w
Fan 1 & light off: 5.3 - 5.4w
Fan 2 & light off: 9.5 - 10.2w
Fan 3 & light off: 15.5 - 16.1w
Fan 4 & light off: 22.3 - 23.0w
Fan 5 & light off: 36.8 - 37.1w
Fan off & light on: 21.2 - 21.6w
Fan 1 & light on: 25.7 - 25.9w
Fan 2 & light on: 30.2 - 30.7w
Fan 3 & light on: 36.4 - 36.5w
Fan 4 & light on: 42.9 - 43.3w
Fan 5 & light on: 55.0 - 57.5w
And here is how I’ve attempted to utilise the code from the other thread, to build sensors to accomplish the first stage:
dining_fan_power:
friendly_name: "Dining Fan Power"
device_class: power
value_template: >-
{% set fan_power = states('sensor.shelly_1pm_dining_room_power') | float %}
{% if 0 < fan_power <= 2 %}
Off
{% elif 21 <= fan_power <= 22 %}
Off
{% elif 5 <= fan_power <= 8.9 %}
1
{% elif 25 <= fan_power <= 29.9 %}
1
{% elif 9 <= fan_power <= 14.9 %}
2
{% elif 30 <= fan_power <= 35.9 %}
2
{% elif 15 <= fan_power <= 20.9 %}
3
{% elif 36 <= fan_power <= 36.8 %}
3
{% elif 22.1 <= fan_power <= 24.9 %}
4
{% elif 42 <= fan_power <= 54.9 %}
4
{% elif 36.9 <= fan_power <= 41.9 %}
5
{% elif 55 <= fan_power <= 60 %}
5
{% endif %}
dining_light_power:
friendly_name: "Dining Light Power"
device_class: power
value_template: >-
{% set light_power = states('sensor.shelly_1pm_dining_room_power') | float %}
{% if 0 < light_power <= 2 %}
Off
{% elif 5 <= light_power <= 8.9 %}
Off
{% elif 9 <= light_power <= 14.9 %}
Off
{% elif 15 <= light_power <= 20.9 %}
Off
{% elif 22.1 <= light_power <= 24.9 %}
Off
{% elif 36.9 <= light_power <= 41.9 %}
Off
{% elif 21 <= light_power <= 22 %}
On
{% elif 25 <= light_power <= 29.9 %}
On
{% elif 30 <= light_power <= 35.9 %}
On
{% elif 36 <= light_power <= 36.8 %}
On
{% elif 42 <= light_power <= 54.9 %}
On
{% elif 55 <= light_power <= 60 %}
On
{% endif %}
It’s a bit convoluted and messy, but it appears to do the job. Understandably the value isn’t stable immediately as there’s an initial surge in power use when stepping up, and there are potential issues where the energy usage is very similar between the fan on speed 3 with the light on, and the fan on speed 5 with the light off - so I don’t want this information to be solely relied upon in real-time by HA for defining the state, but what I would like to do is have HA cross-reference the state against this information periodically, to identify any sync issues.
Here is a screencapture of the front end demonstrating the sensor information being displayed:
This is the code I’m using for my fan components:
fans.yaml
- platform: template
fans:
dining_fan:
friendly_name: "Dining Fan"
value_template: "{{ states('input_boolean.dining_fan_state') }}"
speed_template: "{{ states('input_select.dining_fan_speed') }}"
turn_on:
service: script.dining_fan_on
turn_off:
service: script.dining_fan_off
set_speed:
service: script.dining_fan_set_speed
data_template:
speed: "{{ speed }}"
speeds:
- "lowest"
- "low"
- "medium"
- "high"
- "highest"
input_booleans.yaml
dining_fan_state:
name: Dining Fan State
input_selects.yaml
dining_fan_speed:
name: Dining Fan Speeds
options:
- 'off'
- 'lowest'
- 'low'
- 'medium'
- 'high'
- 'highest'
scripts.yaml
dining_fan_off:
alias: Dining Fan Off
sequence:
- service: switch.turn_on
entity_id: switch.dining_fan_off
- service: input_boolean.turn_off
data:
entity_id: input_boolean.dining_fan_state
dining_fan_on:
alias: Dining Fan On
sequence:
- service: input_boolean.turn_on
data:
entity_id: input_boolean.dining_fan_state
- service_template: >
{% if is_state("input_select.dining_fan_speed", "lowest") %}
script.dining_fan_lowest
{% elif is_state("input_select.dining_fan_speed", "low") %}
script.dining_fan_low
{% elif is_state("input_select.dining_fan_speed", "medium") %}
script.dining_fan_medium
{% elif is_state("input_select.dining_fan_speed", "high") %}
script.dining_fan_high
{% elif is_state("input_select.dining_fan_speed", "highest") %}
script.dining_fan_highest
{% endif %}
dining_fan_set_speed:
alias: Dining Fan Set Speed
sequence:
- service: input_select.select_option
data_template:
entity_id: input_select.dining_fan_speed
option: "{{ speed }}"
- delay:
milliseconds: 500
- service: script.turn_on
entity_id: script.dining_fan_on
dining_fan_lowest:
alias: Dining Fan Lowest
sequence:
- service: switch.turn_on
entity_id: switch.dining_fan_lowest
dining_fan_low:
alias: Dining Fan Low
sequence:
- service: switch.turn_on
entity_id: switch.dining_fan_low
dining_fan_medium:
alias: Dining Fan Medium
sequence:
- service: switch.turn_on
entity_id: switch.dining_fan_medium
dining_fan_high:
alias: Dining Fan High
sequence:
- service: switch.turn_on
entity_id: switch.dining_fan_high
dining_fan_highest:
alias: Dining Fan Highest
sequence:
- service: switch.turn_on
entity_id: switch.dining_fan_highest
switches.yaml
- name: dining_fan_off
command_on: '[code redacted for brevity]'
command_off: '[code redacted for brevity]'
- name: dining_fan_lowest
command_on: '[code redacted for brevity]'
command_off: '[code redacted for brevity]'
- name: dining_fan_low
command_on: '[code redacted for brevity]'
command_off: '[code redacted for brevity]'
- name: dining_fan_medium
command_on: '[code redacted for brevity]'
command_off: '[code redacted for brevity]'
- name: dining_fan_high
command_on: '[code redacted for brevity]'
command_off: '[code redacted for brevity]'
- name: dining_fan_highest
command_on: '[code redacted for brevity]'
command_off: '[code redacted for brevity]'
And for the lights…
lights.yaml
- platform: switch
name: Dining Room
entity_id: switch.dining_room_light
switches.yaml
- name: dining_room_light
command_on: '[code redacted for brevity]'
command_off: '[code redacted for brevity]'
Would anybody be able to suggest how I could go about making HA use the energy consumption information to ensure the state is maintained correctly?
Thank you