Automaction that repeats x times per day

I have been through the other similar topics but I have been unable to modify them to suit

I am very new to home assistant but have been able to get this working.

It monitors my energy consumption via a shelly em and if I start exporting (I have solar) it switches on my hot water immersion heater for 30mins then it loops back and checks for export again

what I would like it to do is only run 4 times each day and send a message to my phone once its run 4 times when send a message to my phone.

Thanks in advance for any help you can give

id: ‘164917665xxxx’
alias: Export test 5
description: ‘’
trigger:

  • platform: homeassistant
    event: start
  • type: power
    platform: device
    device_id: 488fab9b0af4d2cbd4b84e65c596xxxx
    entity_id: sensor.shelly_em_channel_1_power
    domain: sensor
    for:
    hours: 0
    minutes: 2
    seconds: 0
    below: -800
    action:
  • type: turn_on
    device_id: 3552de8c5a7b066650d22b851490xxxx
    entity_id: switch.house_water_heater_socket_1
    domain: switch
  • delay:
    hours: 0
    minutes: 30
    seconds: 0
    milliseconds: 0
  • type: turn_off
    device_id: 3552de8c5a7b066650d22b851490xxxx
    entity_id: switch.house_water_heater_socket_1
    domain: switch
  • service: notify.mobile_app_pauls_iphone
    data:
    message: Hot water has run for 30 mins
    mode: single
1 Like

For a start you need to format your code properly. Please read this, it deals with it.

Also where did you get that automation code, it makes no sense at all.

I have 0 experience with home assistant but this code works, I understand its not pretty but that’s why I am here so that hopefully someone can help. I’ll try and format the code better now

Could you please help with how to copy code from the .yaml file and retain the formatting ?

whenever I copy and paste is loses all the spacing and colour

To format your code all you do is add ``` to the beginning and again at the end… NO biggie, I’ve made worse mistakes posting :slight_smile: OR if you see above when you type there is </> hit that and put your code where it says ‘type or paste code here’ or something like that :slight_smile:

Like this:

- type: entities
          <<: *card_mod_transparent
          entities:
            - sensor.right_kitchen_window_battery
            - binary_sensor.right_kitchen_window_contact
            - sensor.sink_kitchen_window_battery
            - binary_sensor.sink_kitchen_window_contact

It doesn’t copy correctly at times… but go back and look at your code and fix the spacing and things that aren’t right :slight_smile: The above example is spaced incorrectly but that’s how it copied… before I posted [if I had an actual question about it] I would fix it from looking at my code where I copied it. :slight_smile: Like this →

- type: entities
   <<: *card_mod_transparent
   entities:
       - sensor.right_kitchen_window_battery
       - binary_sensor.right_kitchen_window_contact
       - sensor.sink_kitchen_window_battery
       - binary_sensor.sink_kitchen_window_contact

I don’t have time to fully write up yaml but to solve this problem I would use an [input _number](https://www.home-assistant.io/integrations/input_number/) to keep track of how many times the automation turns on. Then in your automation you will have to add a [condition](https://www.home-assistant.io/docs/scripts/conditions/) that checks the numeric_state of your input_number is below 4 and you will also have to add an action that increases the input _number by one.
You will then need another automation that calls a notify service when the input _number reaches four.
You will then need another automation that resets the input _number to zero at midnight or whatever time you want.