Generating automation

Currently I find I’m in the need to generate my automation yamls. For example, I have a few input_datetime (one per day) to set my alarm per day. To automate things I need the same automation entry for every day with the only difference a condition on a different input_datetime entity.

Is there a way of completely generation automations? Normally I would use Jinja for this but jinja is only supported in data_template fields. Is this possible to do without creating a python script that parses the jinja and writes an automation.yaml file?

Could you please post one of the automations you have already in place or explain what these automations should do in the end? Maybe we’ll find a way to do what you want.

Sure, the basic gist of it is this:

input_datetime:
  alarm_mon:
    has_time: true
    name: Alarm

input_boolean:
  enable_alarm_mon:
    name: Alarm Monday

automation:
  - id: 'morning_flow_mon'
    alias: 'morning_flow_mon'
    initial_state: true
    trigger:
      platform: template
      value_template: "{{ states.sensor.time.state == (states.input_datetime.alarm_mon.attributes.timestamp | timestamp_custom('%H:%M', False)) }}"
    condition:
      condition: and
      conditions:
      - condition: time
        weekday:
        - mon
      - condition: state
        entity_id: input_boolea.enable_alarm_mon
        state: 'on'
    action:
    - service: script.morning_workday

Basically copy-paste all of the above for the other weekdays. This really isn’t maintanable and the duplication hurts me as a developer :slight_smile: .

Probably there is a more elegant solution, but you can just have multiple triggers, one for each input_datetime, like this:

automation:
  - id: 'morning_flow_mon'
    alias: 'morning_flow_mon'
    initial_state: true
    trigger:
      - platform: template
        value_template: "{{ states.sensor.time.state == (states.input_datetime.alarm_mon.attributes.timestamp | timestamp_custom('%H:%M', False)) }}"
      - platform: template
        value_template: "{{ states.sensor.time.state == (states.input_datetime.alarm_tue.attributes.timestamp | timestamp_custom('%H:%M', False)) }}"
    condition:
      - condition: state
        entity_id: input_boolean.enable_alarm_mon
        state: 'on'
    action:
      - service: script.turn_on
        entity_id: script.morning_workday

Also your action is wrong, service should be the service needed, in this case “script.turn_on” and you need an entity to act on as well “script.morning_workday”

No, it isn’t.

OP - look at service_template and data_template, this should all be dealt with in one automation.

How is this “action” correct? script.morning_workday is not a valid service, it is the entity_id and the service would be script.turn_on. Also why does he need a service or data template? As far as I understood he wants to start the same script in every automation.

Scripts are, in themselves, services and always have been. That is a valid way to call it. Believe me or don’t, it’s up to you.

I presumed by the name of the script that there was also things like ‘morning day off’ and other such variations.

If OP posts the whole lot I can probably shrink it all down for him quite considerably.

1 Like

Ah ok, didn’t kow that thanks for the information, and sorry to the OP for the misinformation.
If I understood correctly, he wants to trigger the same script every day on the time specified by the different input_datetime entities. However, I just realized now, that he probably only has the time in this input_datetime entities and not the date, so my suggestion would not work.

1 Like

Apologies for the ambiguity. Here are some clarifications:

  • Yes, the syntax is correct :slight_smile:
  • Yes, it’s the same script that needs to be triggered every day
  • Yes, there’s also a morning_off script. Nice one @anon43302295

@Burningstone your assumption is correct. There’s only a time component in the input_datetime so indeed your suggestion wouldn’t work unless there’s a way to set different conditions per trigger but I don’t believe the implementation allows for that.

@anon43302295 I can post the whole lot, but everthing is literally a duplication from the above snippet. The only things that change are all occurences of the string mon to the string tue and equivalent for the remaining days. This is true for the input_datetime, input_boolean and automation.

This morning_working script itself just turns on some lights, sets the thermostat, turs on the hifi for some music, opens the blinds etc.

As a developer you might want to give AppDaemon a try.

https://www.home-assistant.io/docs/ecosystem/appdaemon/

Let’s you do all the automations with Python.
Another possibility would be a script that takes the triggering entity as an input and determines if your script should be started based on the name of the input datetime.
Probably there’s also a solution with templates etc. but I’m not good enough with this, as I barely use them.

No worries, I’ll see what I can do, but it’ll have to wait until I get home :slightly_smiling_face:

1 Like

I had AppDaemon running about a year ago but didn’t like that I had two places for automation and scripts. For simple things yaml is quite nice.
Also I think I had some issues running it in docker or something with nginx (I have home assistant running behind nginx). I can’t really remember.
But I do appreciate the reminder, I’ll see how it improved over this last year! In python this problem would be trivial indeed :slight_smile: .

1 Like

I have the same setup with NGINX and docker, works fine for me :slight_smile: I also didn’t like it in the beginning two have two places for automations, so I switched everything to AppDaemon and would never go back. Scenes are the only thing I kept in home assistant, as for one scene I did, I had some delays when I did them with AppDaemon as compared to home assistant. Maybe this has changed for the better in the mean time.

I think burning is along the right lines but this does not need to go to appdaemon
I presume you have an input boolean for each day and a time for each day too (the above being just a subset of your entities ? yes ? )

I’d go with : -

    trigger:
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.alarm_mon') [0:5] and now().weekday() == 0 and is_state('input_boolean.enable_alarm_mon', 'on') }}"
      - platform: template
        value_template: "{{ states('sensor.time') == states('input_datetime.alarm_tue') [0:5] and now().weekday() == 1 and is_state('input_boolean.enable_alarm_tue', 'on') }}"

Like burning, I only show two triggers but you get the idea, No conditions are required

Edit : try not to use states. and use states() instead, shorter and more fault tolerant. Also you only need to compare strings so the timestamp conversion and formatting is wasted effort

1 Like

Ah, you build the conditions into the triggers, that’s really cheaky :).

I understand. I’ll give this one a try. Thanks! Also thanks for pointing out the states() function, seems this automatation doesn’t use it yet, most of my other ones do.

Most people here on the forum (helping out) hand code everything so the full pallet range is open to us.
The AE is getting there but not sure how the hell it would build a template like that.
I’d recomend that you simpley cut and paste this into your triggers then duplicate for the remaining days adjusting mon --> wed and the weekday number for each one

I was thinking of something similar, but I waited for one of the template cracks, and he arrived :stuck_out_tongue: I just do everything in AppDaemon and I hate Jinja etc. haha that’s why I always suggest AppDaemon.

Well. at least you are not recommending NodeRed :rofl:

I wouldn’t build it into the trigger. An automation and condition like that can be better formatted. If you build the trigger off sensor.time, you can build a nice little template that checks the current day. 1 automation, one condition. Easy to read:

trigger:
- platform: state
  entity_id: sensor.time
condition:
- condition: template
  value_template: >
    {% set time = trigger.to_state.state %}
    {% set day = now().strftime('%a').lower() %}
    {% set datetime = 'input_datetime.alarm_' ~ day %}
    {% set boolean = 'input_boolean.enable_alarm_' ~ day %}
    {{ time == states(datetime) and is_state(boolean, 'on') }}
action:
- service: script.morning_workday

EDIT: this does everyday. To exclude weekends… just add and day not in ['sat','sun'] to the last template. verbatim.

Or you can just use a service template to flip flop for the weekend.

- service_tempate: >
    {% set day = now().strftime('%a').lower() %}
    script.morning_{{ 'weekend' if 'day' ['sat', 'sun'] else 'workday' }}
3 Likes