Automation for Shelly-SmartPlug

Hello, I have a Shelly SmartPlug an I need an automation that covers the follwing things:

  • Start Running at 9am

  • Last run should be 6pm

  • Run only 15min then pause for 1h and then start again

  • Send a notification - I know it will run 15min - how long was it running to my phone

I start with the following code:

alias: Shelly
trigger:
  - platform: time
    at: "09:00:00"
  - platform: time
    minutes: 60
    seconds: 0
    at: "00:00:00"
condition:
  - condition: time
    after: "09:00:00"
    before: "18:00:00"
    weekday:
      - mon
      - tue
      - wed
      - thu
      - fri
      - sat
      - sun
action:
  - service: switch.turn_on
    entity_id: switch.shelly_switch
  - delay: "00:15:00"
  - service: switch.turn_off
    entity_id: switch.shelly_switch
  - service: notify.mobile_app
    data_template:
        message: >
          Die Pool-Pumpe wurde {{ 'eingeschaltet' if is_state('switch.shelly_steckdose', 'on') else 'ausgeschaltet' }}.
          Sie war {{ (now() - trigger.timestamp).total_seconds() // 60 }} Minuten aktiv.mode: single
max_exceeded: silent

But when I try to save it, I get the follwing error message: Message malformed: extra keys not allowed @ data[‘minutes’]

Thanks for help!

Look at lines 6 and 7 of your code. The Time platform of trigger doesn’t support minutes and seconds there?

Ah ok and whats the syntax for a 60min pause?

Maybe it wants 1 hour? Pretty sure I’ve ran into that before where it doesn’t like 60 seconds or minutes…

Lol, thats the magic:

  - platform: time_pattern
    hours: /1

Thanks to everybody for helping!

Ok one more question to this automation…
I didn’t get any notification and i dont know why :-S

Have you considered something like this? I don’t think your if is_state is being executed correctly. I use a trigger id as well as all the times.

alias: Shelly Pool Pumpe
trigger:
  - id "on"
    platform: time
    at: 
    - "09:00:00"
    - "10:00:00"
    - "11:00:00"
    - "12:00:00"
    - "13:00:00"
    - "14:00:00"
    - "15:00:00"
    - "16:00:00"
    - "17:00:00"
    - "18:00:00"
  - id "off"
    platform: time
    at: 
    - "09:15:00"
    - "10:15:00"
    - "11:15:00"
    - "12:15:00"
    - "13:15:00"
    - "14:15:00"
    - "15:15:00"
    - "16:15:00"
    - "17:15:00"
    - "18:15:00"
condition:
action:
  - service: switch.turn_{{ trigger_id }}
    entity_id: switch.shelly_switch

  - service: notify.mobile_app_<your device ID here>   <------- this is from documentation and think you need ID
    data:
        message: >
          {% if states('switch.shelly_steckdose') == 'on' %} 
            "Die Pool-Pumpe wurde eingeschaltet. Sie War {{ (now() - trigger.timestamp).total_seconds() / 60}} Minuten aktiv.mode: single"
          {% else %}
            "Die Pool-Pumpe wurde ausgeschaltet. Sie War {{ now() - trigger.timestamp).total_seconds() / 60 }} Minuten aktiv.mode: single"
          {% endif %}
max_exceeded: silent

I tried your notification part and get the followin error message:

Message malformed: template value should be a string for dictionary value @ data['action'][3]['data']

Do you have this in your YAML configuration

# Example configuration.yaml entry
sensor:
  - platform: time_date
    display_options:
      - 'time'
      - 'date'
      - 'date_time'
      - 'date_time_utc'
      - 'date_time_iso'
      - 'time_date'
      - 'time_utc'
      - 'beat'

I recommend you try it with just the words first.

so like this as a test

"Die Pool-Pumpe wurde eingeschaltet. Sie War Minuten aktiv.mode: single"

Did you add the device id as specified in the documentation. You should have also deleted my comment.
I was not specific so I thought I should clarify.

So from this

service: notify.mobile_app_<your device ID here>   <------- this is from documentation and think you need ID

to this:

service: notify.mobile_app_pixel_7_pro

Hope this helps

hmm ok my if statement is the problem, without it, it works…

any idea how can i workaround it ? my configuration-file i already extend it and try again, same issue…

I did some looking and there is no trigger.timestamp. However there is a trigger.now. It is defined as:

DateTime object that triggered the time trigger.

so I wrote a modified script in developer tools → templates like this

{% set trigger = now() %}
{% if states('switch.area_cans') == 'on' %} 
  "Die Pool-Pumpe wurde eingeschaltet. Sie War {{ (as_timestamp(now()) - as_timestamp(trigger)) | float(0) / 60 }} Minuten aktiv.mode: single"
{% else %}
   "Die Pool-Pumpe wurde ausgeschaltet. Sie War {{ (as_timestamp(now()) - as_timestamp(trigger)) | float(0) / 60 }} Minuten aktiv.mode: single"
{% endif %}

This will basically display a small number of minutes and here is the result

as_timestamp converts the string datetime to a float of the number of seconds from jan 1 1970.

you need to change trigger in the yaml script to trigger.now like this:

{% if states('switch.shelly_steckdose') == 'on' %} 
  "Die Pool-Pumpe wurde eingeschaltet. Sie War {{ (as_timestamp(now()) - as_timestamp(trigger.now)) | float(0) / 60 }} Minuten aktiv.mode: single"
{% else %}
   "Die Pool-Pumpe wurde ausgeschaltet. Sie War {{ (as_timestamp(now()) - as_timestamp(trigger.now)) | float(0) / 60 }} Minuten aktiv.mode: single"
{% endif %}

Here is a screenshot of the templates page

Hy, i really dont know what i am doing wrong … When i test the code in dev tools, it works but in my automation not :-S

Can you please post the automation (formatted) as you have it installed.