# Help Please - newby requires assistance to ensure his automation runs just once a day! Thanks in advance

Hi everyone

I am new to this so please be kind :slight_smile:

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:

  • type: battery_level

platform: device

device_id: xxx

entity_id: xxx

domain: sensor

above: 60

below: 100

condition:

  • condition: sun

after: sunrise

action:

  • type: turn_on

device_id: xxxx

entity_id: xxx

domain: switch

  • delay:

hours: 2

minutes: 0

seconds: 0

milliseconds: 0

  • type: turn_off

device_id: xxxx

entity_id: xxx

domain: switch

mode: single

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:

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.

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 :slightly_smiling_face:

- 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

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.

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

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

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