So I’ve been trying to add some extra functionality to my “going to bed” automation.
Trigger is me closing my bedroom door. Then some lights would turn off and the bed light would turn on.
Now I recently added a power switch to my TV which also measures power.
What I want to do is have the switch turn off if the power is below lets say 40W, and else send me a notification that the TV is on (so not to bluntly turn off everything when I close the door by accident or something).
I thought I could do this with templating but can’t seem to figure out how exactly. The difficulty I’m having is that I either want to turn on a switch OR send a notification based on the same condition being either true or false.
This is what I have now:
- alias: 'Going to bed'
trigger:
- platform: state
entity_id: binary_sensor.contact
to: 'off'
condition:
- condition: or
conditions:
- condition: time
after: '22:00:00'
- condition: time
before: '04:00:00'
action:
- service: light.turn_off
data:
entity_id:
- light.bank
- light.keuken
- service: light.turn_on
data:
entity_id:
- light.slaapkamer_2
- light.bidside_lamp_2
- service_template: >
{% if states('sensor.blitzwolf_01_wattage') | float < 40 %}
switch.turn_off
{% else %}
{% endif %}
entity_id: switch.blitzwolf_01_relay
- service: notify.hassbot
data_template:
message: >
{% if states('sensor.blitzwolf_01_wattage') | float < 40 %}
{% else %}
"TV is nog aan!"
{% endif %}
entity_id: notify.hassbot
This doesn’t work because it sends empty commands.
I have the feeling that I’m overthinking this but can’t find a better way to implement this. Any help would be greatly appreciated!
This is only a guess, but any time I’ve tried to use time after: and before: there’s usually an issue. Try commenting/removing the before: ‘04:00:00’ condition and see if it works. If so, the rest of your definition is good.
You can’t set service_template to nothing, it must be a valid service. However, what you can do is specify a non-existent entity. Home Assistant won’t complain if you attempt to turn off a switch that doesn’t exist.
We can leverage that information to achieve what you want:
@FredTheFrog
I actually changed it from an or condition with two separate conditions for before and after, which used to work fine. But searching for a solution for my problem I came across a post which used both after and before in a single condition so I figured it would be fine. I changed it back to what I had as to make sure that’s not the issue. The error is still there.
@123 Yeah that’s what I feared. I tried your code but got a syntax error:
Invalid config for [automation]: not a valid value for dictionary value @ data[‘action’][2][‘entity_id’]. Got None. (See /config/configuration.yaml, line 269). Please check the docs at https://home-assistant.io/components/automation/
My mistake. I’ve corrected the example. There’s no longer a need to indicate service_template (just service) and now data_template is needed because entity_id contains a template.