I tend to agree with the binary sensor because it makes the automation simple. But it requires some advanced templating. Either way, people can help you with that. To hit this approach, start by integrating sensor.time.
sensor:
- platform: time_date
display_options:
- 'time'
This sensor updates every minute of the day and has an entity_id of sensor.time
.
Next, create a binary_sensor.
binary_sensor:
- platform: template
sensors:
unit_2_helper:
friendly_name: Unit 2 Automation Helper
entity_id: sensor.time
value_template: >
{% set is_home = is_state('device_tracker.moto_g_6', 'home') %}
{{ is_home and 19 <= now().hour < 23 }}
Now this will create a binary_sensor that is on when you are home and between the hours of 19 and 23. Lets break down the template:
This line simply sets is_home
to true or false based on device_tracker.moto_g_6 being home or not.
{% set is_home = is_state('device_tracker.moto_g_6', 'home') %}
This line returns 'on'
or 'off'
to the binary_sensor. It will be 'on'
when is_home
is True
and when the current hour is between 19 and 23 (including 19 and 23).
{{ is_home and 19 <= now().hour < 23 }}
Now that we have our binary_sensor.unit_2_helper
, we can make the automations.
- alias: Turn On Unit 2
trigger:
- platform: state
entity_id: binary_sensor.unit_2_helper
to: 'on'
action:
- service: switch.turn_on
entity_id: switch.on_off_plug_in_unit_2
- alias: Turn Off Unit 2
trigger:
- platform: state
entity_id: binary_sensor.unit_2_helper
to: 'off'
action:
- service: switch.turn_off
entity_id: switch.on_off_plug_in_unit_2
And if you want to get your toes wet, here’s 1 automation that does it all (but uses templates).
- alias: Turn On/Off Unit 2
trigger:
- platform: state
entity_id: binary_sensor.unit_2_helper
action:
- service_template: switch.turn_{{ trigger.to_state.state }}
entity_id: switch.on_off_plug_in_unit_2