paul5464
(Paul5464)
June 7, 2024, 11:47am
1
Hi everyone
I am new to this so please be kind
I have created an automation to activate a switch once a battery level exceeds 60% charged. It works. However, i want to make sure that this is only triggered once each day. For example, if it is triggered, then later in the day the battery goes down below 60% before being charged again, i dont want it to trigger when it hits 61% charged.
How do i edit the below code to achieve the desired result? (i’ve taken the device and entity ids out for privacy reasons)
alias: Switch on water heater when solar above %
description: “”
trigger:
platform: device
device_id: xxx
entity_id: xxx
domain: sensor
above: 60
below: 100
condition:
after: sunrise
action:
device_id: xxxx
entity_id: xxx
domain: switch
hours: 2
minutes: 0
seconds: 0
milliseconds: 0
device_id: xxxx
entity_id: xxx
domain: switch
mode: single
Troon
(Troon)
June 7, 2024, 11:54am
2
Welcome.
Firstly, format your code correctly. Do this:
```
CODE HERE
```
Then it’ll look like this:
CODE HERE
with correct indentation and no silly formatting. See here:
Before we begin…
This forum is not a helpdesk
The people here don’t work for Home Assistant, that’s an open source project. We are volunteering our free time to help others. Not all topics may get an answer, never mind one that helps you solve your problem.
[image]
This also isn’t a general home automation forum, this is a forum for Home Assistant and things related to it. Any question about Home Assistant, and about using things with Home Assistant, is welcome here. We can’t help you with eve…
To get an automation to run once per day only, add a template condition with a value template of:
{{ this.attributes.last_triggered.date() != now().date() }}
so if you’re editing that code rather than doing it via the UI, add this before or after your sun
condition:
- condition: template
value_template: "{{ this.attributes.last_triggered.date() != now().date() }}"
Also, don’t conceal device or entity IDs. There’s no privacy risk but you might be hiding other issues.
paul5464
(Paul5464)
June 7, 2024, 12:26pm
3
Thank you for taking the time to reply. I do appreciate the learning from it.
I have copied in the following after my sun condition as you kindly suggest
- condition: template
value_template: "{{ this.attributes.last_triggered.date() != now().date() }}"
However, when i go to save, i get this error message
“Message malformed: extra keys not allowed @ data[‘condition’][1][‘after’]”
What am i doing wrong?
full code reads as follows
alias: Switch on water heater when solar battery above %
description: ""
trigger:
- type: battery_level
platform: device
device_id: 4a1f34a3802562d798a870b015177638
entity_id: 174763093e22bdbf4622f040f58e1929
domain: sensor
above: 60
below: 100
condition:
- condition: sun
- condition: template
value_template: "{{ this.attributes.last_triggered.date() != now().date() }}"
after: sunrise
action:
- type: turn_on
device_id: bb96378c3812f80f6f66c736d8a0f718
entity_id: dfd5869d4db3dca16673c2895f786f4b
domain: switch
- delay:
hours: 2
minutes: 0
seconds: 0
milliseconds: 0
- type: turn_off
device_id: bb96378c3812f80f6f66c736d8a0f718
entity_id: dfd5869d4db3dca16673c2895f786f4b
domain: switch
mode: single
Troon
(Troon)
June 7, 2024, 12:29pm
4
You put it in the middle of your sun
condition.
condition:
- condition: sun
after: sunrise
- condition: template
value_template: "{{ this.attributes.last_triggered.date() != now().date() }}"
Those are two separate conditions:
is it after sunrise?
is the last_triggered
date different from today’s date?
If both are true, then the flow will continue to the action
.
I’d strongly suggest just using the UI if that wasn’t obvious to you.
paul5464
(Paul5464)
June 7, 2024, 2:19pm
5
Thank you Troon.
I used the visual editor. Code now reads as per below. Do you think the below should achieve the desired result? Appears to have saved correctly.
alias: Switch on water heater when solar battery above %
description: ""
trigger:
- type: battery_level
platform: device
device_id: 4a1f34a3802562d798a870b015177638
entity_id: 174763093e22bdbf4622f040f58e1929
domain: sensor
above: 60
below: 100
condition:
- condition: sun
after: sunrise
- condition: template
value_template: "{{ this.attributes.last_triggered.date() != now().date() }}"
action:
- type: turn_on
device_id: bb96378c3812f80f6f66c736d8a0f718
entity_id: dfd5869d4db3dca16673c2895f786f4b
domain: switch
- delay:
hours: 2
minutes: 0
seconds: 0
milliseconds: 0
- type: turn_off
device_id: bb96378c3812f80f6f66c736d8a0f718
entity_id: dfd5869d4db3dca16673c2895f786f4b
domain: switch
mode: single
Troon
(Troon)
June 7, 2024, 2:56pm
6
Should do, with the proviso that the 2-hour delay won’t survive an HA restart. If you restart your system during that two hours, the switch will not get turned off.
Numerous ways around this:
set a timer and use that to turn the switch off
separate automation to turn the switch off after its been on 2 hours and at HA restart (safety over savings)
set an input_datetime
to the desired off time and use a separate automation
paul5464
(Paul5464)
June 7, 2024, 4:17pm
7
Thank you Troon.
Once again, very helpful.
Have learnt a lot from our interactions today. Got HA a few weeks. Had only done the most basic of automations until this point.
This was my first attempt at linking the state of one device to trigger an action in another device subject to certain conditions!
As mentioned, the above automation will work, however if HA gets rebooted or the automations get reloaded due to a change the delay will fail and the heater will stay on indefinitely
Here is a example of using a Timer to switch off the pool heater after 2 hours, this will service a reboot and should work as long as HA is up and running before the 2 hours are up. This will require you to create a Helper - Timer to make this one work.
description: "Switch On Pool Heater - 2 Hour Timer"
mode: single
trigger:
- platform: event
event_type: timer.finished
event_data:
entity_id: timer.pool_heater
id: timer_complete_pool_heater
- platform: numeric_state
entity_id:
- sensor.solar_battery_1
above: 60
condition: []
action:
- choose:
- conditions:
- condition: trigger
id:
- timer_complete_pool_heater
sequence:
- type: turn_off
device_id: 942a2a1eb33375a650c3a5d99935834b
entity_id: d90dd4117be48049fcb1604b36726a1f
domain: switch
- conditions:
- condition: template
value_template: "{{ this.attributes.last_triggered.date() != now().date() }}"
- condition: state
entity_id: timer.pool_heater
state: idle
sequence:
- service: timer.start
metadata: {}
data:
duration: "02:00:00"
target:
entity_id: timer.pool_heater
- type: turn_on
device_id: 942a2a1eb33375a650c3a5d99935834b
entity_id: d90dd4117be48049fcb1604b36726a1f
domain: switch