So I guess some of it makes sense .
Then I can also learn how I can do an If else inside of an If, which I think would help me understand the benefits of this (if thatâs something you will use in your chuck)
Yeah, not a problem. Iâll write out how I would do it and (hopefully!) detail how it works and hopefully that will help you understand what youâre reading
OK, so the first thing Iâve done is simplify your flow chart, because lower down youâre actually asking questions that are duplicitous because theyâre already filtered by the conditions above. I got it down to
Single click when sun below horizon
|
Is it before 2200? - - - - - - yes
| |
No turn off light
|
Run the script
So, I used the single click as the trigger and a condition to stop the automation firing if sun is above horizon.
That way we only have to deal with is it before or after 2200 hours.
Then I used a service template and a data template so if it is before 2200 you get
service: homeassistant.turn_off
entity_id: light.LIGHT
But after you get
service: homeassistant.turn_on
entity_id: script.PATH_LIGHT_AND_OTHER
Which looks likeâŚ
trigger:
platform: state
entity_id: XIAOMI_BUTTON
to: SINGLE_CLICK
condition:
condition: template
value_template: "{{ is_state('sun.sun' , 'below_horizon') }}"
action:
service_template: >
{% if now().hour < 22 %} homeassistant.turn_off
{% else %} homeassistant.turn_on {% endif %}
data_template:
entity_id: >
{% if now().hour < 22 %} light.LIGHT
{% else %} script.PATH_LIGHT_AND_OTHER {% endif %}
Iâve also just realised that where youâve put
Is the light on? > Yes/no > turn off light
You might have actually meant to turn it on if was off, and off if it was on.
If thatâs the case change line 10 from homeassistant.turn_off
to homeassistant.toggle
Yes you are right about that!
One question though, so Iâve changed the light because I wanted it to either turn on or off as you mentioned above.
- alias: Xiaomi
trigger:
platform: event
event_type: xiaomi_aqara.click
event_data:
entity_id: binary_sensor.switch_158d00014d0cc0
click_type: single
condition:
condition: template
value_template: "{{ is_state('sun.sun' , 'below_horizon') }}"
action:
service_template: >
{% if now().hour < 22 %} homeassistant.toggle
{% else %} homeassistant.turn_on {% endif %}
data_template:
entity_id: >
{% if now().hour < 22 %} light.led_ibox
{% else %} script.nightlight {% endif %}
So does it look like we are good to go?
Side question, if I wanted to run another script between âbelow horizonâ and 22, then I just have to switch the line with: homeassistant.turn_on and then add script.xxx on the same line below data template - am I right?
action:
service_template: >
{% if now().hour < 22 %} homeassistant.turn_o
{% else %} homeassistant.turn_on {% endif %}
data_template:
entity_id: >
{% if now().hour < 22 %} script.ONE
{% else %} script.TWO {% endif %}
Looks good to go to me, presuming that trigger is correct
If all youâre launching is scripts you can just use a service template
action:
service_template: >
{% if now().hour < 22 %} script.ONE
{% else %} script.TWO {% endif %}
No extra data required
Arh yes makes sense! Learning a lot here!
So when I use the homeassistant.toggle then it simply turns on the light at previously known value and turn it off if on and the other way around.
If I wanted to add such info, brightness etc. When turning on, then I should state that in the data_template?
Yes, but thatâs where it gets in to the territory of using an external script to do it because otherwise the template wonât resolve properly, so like before 10pm youâll get
service: light.turn_on
entity_id: light.ibox
brightness_pct: 60
Which is fine, but thereâs no way to remove the brightness_pct data, so after 10pm you get
service: script.turn_on
entity_id: script.bedtime
brightness_pct:
Which is invalid, because script.turn_on doesnât accept a brightness parameter.
Hence why I said you need to get the flow chart right first, because as soon as you slightly change the flow chart it massively affects how we can automate it. It can always be done, but the code to do it can be drastically different from last time.
Yes absolutely.
Just trying to figure out when you should call scripts, when you should use service_template and when you should use the data_template.
I will try with the above, hopefully you can get some rest while i fool around
Hope you are up for some other challenges later
Always love a challenge
Sounds good.
Unfortunately when pasting the automation into my automation file I get an error:
Invalid config for [automation]: [data_template] is an invalid option for [automation]. Check: automation->data_template. (See /config/configuration.yaml, line 17). Please check the docs at Automation - Home Assistant
EDIT: Never mind - I got it accepting it. I had to change some of the spacing at the beginning of the data_template.
So I tested it and the first part is running with the light toggle. But here after 22 (10pm), it doesnât run the script, but keeps toggling the light.
Do you have any idea why?
Is the time right on your device and the timezone set correctly in homeassistant?
Yes - it shows the correct time.[quote=âmf_social, post:33, topic:83466, full:trueâ]
Is the time right on your device and the timezone set correctly in homeassistant?
[/quote]
Yes time affects my region and it shows the correct time.
I have no idea then, the template resolves that if the hour returned by now() is less than 22 it gives the light result, and 22 or above the script result.
Stick it in the template editor and see what it says and move the hour around to see what is returning?
Template editor?
Here is the âworking codeâ did I miss anything else?
- alias: Xiaomi
trigger:
platform: event
event_type: xiaomi_aqara.click
event_data:
entity_id: binary_sensor.switch_158d00014d0cc0
click_type: single
condition:
condition: template
value_template: "{{ is_state('sun.sun' , 'below_horizon') }}"
action:
service_template: >
{% if now().hour < 22 %} homeassistant.toggle
{% else %} homeassistant.turn_on {% endif %}
data_template:
entity_id: >
{% if now().hour < 22 %} light.ibox
{% else %} script.nightlight {% endif %}
On the sidebar, looks like a sheet of paper with <> on it.
The automation certainly looks fine from here.
Template working fine here, if I leave it at 22 (itâs just after midday here) it would toggle the lightâŚ
But if I change it to twelve, which weâre now after, it would activate the scriptâŚ
Didnât know that feature! SO useful!
It shows the exact same result as on yours. HmmâŚ
Will try again when I get home, but could it be related to the condition then?
Well, no, because the condition will stop the whole automation being run so it wonât get as far as the action (template or no template).
Thereâs no other automations superceding this one is there?