Geekzone: technology news, blogs, forums
Guest
Welcome Guest.
You haven't logged in yet. If you don't have an account you can register now.


peejayw

1757 posts

Uber Geek


#311760 12-Feb-2024 14:00
Send private message quote this post

How would I go about producing an automation that will turn on a switch every second day at a set time? Thanks.





 I'm supposed to respect my elders, but it's getting harder and harder for me to find one now.


Create new topic
amanzi
Amanzi
1176 posts

Uber Geek

ID Verified
Trusted
Lifetime subscriber

  #3194331 12-Feb-2024 14:09
Send private message quote this post

Editing this post to remove the faulty AI-generated code...

 

This automation should work - although it will still need to be tested.

 

alias: Turn on switch every second day at 8 PM

 

description: ""

 

trigger:

 

- platform: time

 

at: "20:00:00"

 

condition:

 

- condition: template

 

value_template: "{{ now().day % 2 == 0 }}"

 

action:

 

- service: switch.turn_on

 

target:

 

entity_id: switch.your_switch_entity_id

 

data: {}

 

mode: single

 

 


 
 
 

Trade NZ and US shares and funds with Sharesies (affiliate link).
peejayw

1757 posts

Uber Geek


  #3194332 12-Feb-2024 14:16
Send private message quote this post

Thats impressive, AI at work. Will try and let you know. Thanks.





 I'm supposed to respect my elders, but it's getting harder and harder for me to find one now.


amanzi
Amanzi
1176 posts

Uber Geek

ID Verified
Trusted
Lifetime subscriber

  #3194334 12-Feb-2024 14:21
Send private message quote this post

The only downside to this approach is that it will only match dates that are divisible by 2, e.g. 2nd, 4th, 6th, 8th, etc. If you wanted it to trigger on odd days instead, then you'd need to use a template condition type with: {{ (now().day % 2) == 1 }}




peejayw

1757 posts

Uber Geek


  #3194354 12-Feb-2024 14:48
Send private message quote this post

I tried that suggestion, got the following error message......

 

 

 

Message malformed: extra keys not allowed @ data['days']





 I'm supposed to respect my elders, but it's getting harder and harder for me to find one now.


amanzi
Amanzi
1176 posts

Uber Geek

ID Verified
Trusted
Lifetime subscriber

  #3194360 12-Feb-2024 15:13
Send private message quote this post

Ah - interesting. Looks like the time-pattern doesn't support "days". The Google AI is not that impressive :-)

 

Try use the "template" conditional instead:

 

alias: Turn on switch every second day at 8 PM

 

description: ""

 

trigger:

 

- platform: time

 

at: "20:00:00"

 

condition:

 

- condition: template

 

value_template: "{{ now().day % 2 == 0 }}"

 

action:

 

- service: switch.turn_on

 

target:

 

entity_id: switch.your_switch_entity_id

 

data: {}

 

mode: single

 

 

 


peejayw

1757 posts

Uber Geek


  #3194372 12-Feb-2024 15:50
Send private message quote this post

Thanks, tried that with the same error message as before.





 I'm supposed to respect my elders, but it's getting harder and harder for me to find one now.


amanzi
Amanzi
1176 posts

Uber Geek

ID Verified
Trusted
Lifetime subscriber

  #3194377 12-Feb-2024 16:09
Send private message quote this post

That might be an issue on your side. I tried the above code in my Home Assistant and it loaded up fine. Should look something like this:

 




SpookyAwol
612 posts

Ultimate Geek


  #3194379 12-Feb-2024 16:17
Send private message quote this post

Instead of messing around with days, you could just use a boolean helper.

Day 1, If boolean off, set boolean to true, turn on lights.

 

Day 2, if boolean true, turn off


peejayw

1757 posts

Uber Geek


  #3194380 12-Feb-2024 16:18
Send private message quote this post

OK, so instead of entering as yaml I used the visual editor and copied from your screenshot and that was accepted without errors.

 

So all going well it should not trip tomorrow and turn on Wednesday, will advise.

 

Thanks so much for your help.





 I'm supposed to respect my elders, but it's getting harder and harder for me to find one now.


eluSiveNZ
180 posts

Master Geek


  #3194384 12-Feb-2024 16:29
Send private message quote this post

Would it be easier to create a calendar event (within the Home Assistant calendar) set to repeat every second day, and use that as the trigger in the automation?


peejayw

1757 posts

Uber Geek


  #3194385 12-Feb-2024 16:33
Send private message quote this post

SpookyAwol:

 

Instead of messing around with days, you could just use a boolean helper.

 

 

I need to brush up on my booleans 😬





 I'm supposed to respect my elders, but it's getting harder and harder for me to find one now.


pih

pih
585 posts

Ultimate Geek

Lifetime subscriber

  #3194389 12-Feb-2024 16:52
Send private message quote this post

Down side of the suggested response is that on months with odd numbers of days (29/31) you'll get the automation triggering either twice in a row (triggering on odd days) or not triggering for 2 days (of triggering on even days). The boolean toggle might work better but won't remember your "schedule" if it's disabled/skipped for a day. If you really want to make sure it sticks to a fixed day-on-day-off schedule, do it based on epoch instead. Something like this (also AI generated, untested):


value_template: "{{ (now() - as_datetime('19700101T00Z')).days % 2 == 0 }}"


neb

neb
9368 posts

Uber Geek

Trusted
Lifetime subscriber

  #3194410 12-Feb-2024 18:27
Send private message quote this post

Sort of a meta-question but it does apply to the above, does anyone know why HA tries to use a markup language as a scripting language? If I was going to script something for automation, YAML is pretty much the last tool I'd reach for, every single thing you want to do with HA becomes an awkwardly painful exercise in lateral thinking as you try and guess how you'd express something like a control loop using a mechanism that can't do that but can only do markup.

amanzi
Amanzi
1176 posts

Uber Geek

ID Verified
Trusted
Lifetime subscriber

  #3194412 12-Feb-2024 18:36
Send private message quote this post

neb: Sort of a meta-question but it does apply to the above, does anyone know why HA tries to use a markup language as a scripting language? If I was going to script something for automation, YAML is pretty much the last tool I'd reach for, every single thing you want to do with HA becomes an awkwardly painful exercise in lateral thinking as you try and guess how you'd express something like a control loop using a mechanism that can't do that but can only do markup.

 

I'm not a fan of YAML at all, but all this is doing is describing the automation in an "if this then that" type of manner. So the YAML markup is not actually doing any scripting, although you can include template blocks which are based on Jinja which means you can include Python-like code in the YAML files.


Handle9
10119 posts

Uber Geek

Trusted
Lifetime subscriber

  #3194467 13-Feb-2024 07:34
Send private message quote this post

SpookyAwol:

 

Instead of messing around with days, you could just use a boolean helper.

Day 1, If boolean off, set boolean to true, turn on lights.

 

Day 2, if boolean true, turn off

 

 

That's how I'd do it. Toggle the boolean on a schedule then follow with an if statement


Create new topic





News and reviews »

One New Zealand Extends 3G Switch-off Date
Posted 11-Apr-2024 08:56


Amazon Echo Hub Review
Posted 10-Apr-2024 18:57


Epson Launches New Versatile A4 Desktop Scanners
Posted 10-Apr-2024 15:31


Motorola Mobility Launches New Android Phones in New Zealand
Posted 10-Apr-2024 14:59


Logitech G Unveils the PRO X 60 Gaming Keyboard
Posted 9-Apr-2024 19:01


Logitech Unveils Signature Slim Keyboard and Combo
Posted 9-Apr-2024 13:33


ExpressVPN Launches Aircove Go Portable Router With Built-in VPN
Posted 26-Mar-2024 21:25


Shure MoveMic Review
Posted 25-Mar-2024 12:47


reMarkable 2 Launches at JB Hi-Fi New Zealand
Posted 20-Mar-2024 08:36


Samsung Galaxy S24 Ultra review
Posted 19-Mar-2024 11:37


Google Nest Wifi Pro Review
Posted 16-Mar-2024 11:28


Samsung Galaxy A55 5G and Galaxy A35 5G
Posted 12-Mar-2024 12:41


Cricut EasyPress Mini Zen Blue launches at Spotlight New Zealand
Posted 12-Mar-2024 12:32


Logitech Introduces MX Brio Webcam
Posted 12-Mar-2024 12:24


HP Unveils Broadest Consumer Portfolio of AI-Enhanced Laptops
Posted 3-Mar-2024 18:09









Geekzone Live »

Try automatic live updates from Geekzone directly in your browser, without refreshing the page, with Geekzone Live now.



Are you subscribed to our RSS feed? You can download the latest headlines and summaries from our stories directly to your computer or smartphone by using a feed reader.







GoodSync is the easiest file sync and backup for Windows and Mac