MontyRisk
(Monty Risk)
August 11, 2020, 12:02pm
1
Just looking for a code check here. Trying to make a binary sensor that turns on as follows:
30 min from sunset & > 30% cloudy
or
sun elevation = -5
Does this look right?
- platform: template
sensors:
time_logic:
value_template: >-
"{{ states('sensor.owm_cloud_coverage')| float >= 30
and states('sensor.time') == as_timestamp(state_attr('sun.sun', 'next_setting')) | timestamp_custom('%R')
or state_attr('sun.sun', 'elevation') | float <=-5}}"
petro
(Petro)
August 11, 2020, 12:11pm
2
seems a bit odd. It will only turn on for 1 minute.
MontyRisk
(Monty Risk)
August 11, 2020, 3:59pm
3
Yea, this sensor will turn on with said criteria. I have lights that will turn on when this sensor turns on.
petro
(Petro)
August 11, 2020, 4:01pm
4
Well, if you have a sensor that turns on and off at the correct times… you can just make as simple automation:
- alias: lights with binary sensor
trigger:
- platform: state
entity_id: binary_sensor.xxxxx
action:
- service: light.turn_{{ trigger.to_state.state }}
entity_id: light.xxxxx
MontyRisk
(Monty Risk)
August 12, 2020, 12:00pm
5
Thats the plan, thank you!
petro
(Petro)
August 12, 2020, 12:42pm
6
Well, I can help you tailor the template if you want to add an ending time to it. Just let me know your ideas.
MontyRisk
(Monty Risk)
August 12, 2020, 8:09pm
7
@petro , I appreciate it, but what I really need is to confirm that the value template in my first post will do as I have listed. I new to writing templates and was more so looking for a peer review.
I have several lighting templates for different things and the turn on logic is redundant. I am going to a sensor to hold the turn on logic. Then in my lighting automation’s, when the sensor turns on, then turn on what is needed.
petro
(Petro)
August 12, 2020, 8:16pm
8
your template is fine, just remove the outer quotes because you’re using a multiline template