Help with Semi-Advanced Automation for Generator

Newish-Bee. I have several simple automations created with the Visual tool. This one seems to possibly go beyond (at least with a reasonable number of steps). The flow is simple if only I were programing in C. I am semi-competent in YAML. My entire Dash is custom YAML with some binary input logic and some Jinga2 and Java Templating.

All the Shelly… are devices - Some Switches and some Binary_Inputs

Attached are the two automations (or maybe could be 1??) I’m trying to create:

I would create two automations to start with. You might be able to combine them later but many (most?) times I don’t bother unless it is very simple and doesn’t impact readability and maintenance. there’s nothing wrong with using two automations.

it’s better to try to figure it out first and if it doesn’t work then post actual code (in text instead of screenshots - it makes it easier for others to copy/paste/edit instead of having to type everything out by hand) and hopefully someone can help you out.

Sorry, I was looking for some sort of direction. Not for someone to do the work. So, thank you for the suggestion to use two automations. I have it working with the menu driven automations now, but since the code generator uses codes for device_id and entity_id its hard to edit the code. I found the device_id in the URL when i examine my various devices. However when i examine my entities I cannot figure out the ID. I think I have to leave the encoded device_ids and entity_ids and not switch to the “normal” names to allow me to use the HA automation builder.

My code looks like junk and is super “brute force”.

Right now i’m using a DELAY command when I would like to use a WAIT command, but the WAIT does not work as expected. Maybe I’ll try some more then maybe ask for help on the WAIT problem.

Am I correct to assume i have to leave the Device_ids" and Entity_ids for the automation tool to work? Is there a to look up any entitys ID?

Thank you, Mac

alias: Gen Start
description: ""
trigger:
  - type: powered
    platform: device
    device_id: fb0ab16fbe296d2a6f4ed65b20013d88
    entity_id: aef0db9b684d219b79c1da7cbd95f0d6
    domain: binary_sensor
condition: []
action:
  - if:
      - type: is_powered
        condition: device
        device_id: fb0ab16fbe296d2a6f4ed65b20013d88
        entity_id: 0c5191974d52b45fd407943b75d2d7d2
        domain: binary_sensor
    then:
      - type: turn_on
        device_id: f64d6b29b7be680ba58bd9c65571b2a5
        entity_id: 7ab167660a4620e1575494471936f6d7
        domain: switch
      - delay:
          hours: 0
          minutes: 0
          seconds: 30
          milliseconds: 0
      - if:
          - type: is_powered
            condition: device
            device_id: fb0ab16fbe296d2a6f4ed65b20013d88
            entity_id: 0c5191974d52b45fd407943b75d2d7d2
            domain: binary_sensor
        then:
          - type: turn_on
            device_id: f64d6b29b7be680ba58bd9c65571b2a5
            entity_id: 7ab167660a4620e1575494471936f6d7
            domain: switch
          - delay:
              hours: 0
              minutes: 0
              seconds: 30
              milliseconds: 0
          - if:
              - type: is_not_powered
                condition: device
                device_id: fb0ab16fbe296d2a6f4ed65b20013d88
                entity_id: 0c5191974d52b45fd407943b75d2d7d2
                domain: binary_sensor
            then:
              - type: turn_on
                device_id: 0fc58f223f80f6a6fc6ba37081948581
                entity_id: 2ce7a1c1043e1234dee2748de941fa9f
                domain: switch
              - type: turn_on
                device_id: 30f78d70b7b308fdd3b86487c67e0da7
                entity_id: defefbccc9bfa91ba0545e27a3ab8637
                domain: switch
        else:
          - type: turn_on
            device_id: 0fc58f223f80f6a6fc6ba37081948581
            entity_id: 2ce7a1c1043e1234dee2748de941fa9f
            domain: switch
          - type: turn_on
            device_id: 30f78d70b7b308fdd3b86487c67e0da7
            entity_id: defefbccc9bfa91ba0545e27a3ab8637
            domain: switch
    else:
      - type: turn_on
        device_id: 0fc58f223f80f6a6fc6ba37081948581
        entity_id: 2ce7a1c1043e1234dee2748de941fa9f
        domain: switch
      - type: turn_on
        device_id: 30f78d70b7b308fdd3b86487c67e0da7
        entity_id: defefbccc9bfa91ba0545e27a3ab8637
        domain: switch
mode: single

no problem.

no you can use entity_id’s but you need to use states platform/conditions and services for the actions. those different options are available thru the UI dropdown.

And most of the advanced users use those instead of the device_id stuff because of some limitations using those.

one being that you can’t edit/create a device “thing” manually.

yes you can go to developers tools->states tab and look for the entity id of stuff there.

I have to say that those entity_ids look super weird. But I never use the automation editor so that may be normal depending on how the integration creates the entities.

without knowing the entity_id’s of the entities this is basically how the automation you have as written above would look using states/conditions and services:

alias: Gen Start
trigger:
  - platform: state
    entity_id: binary_sensor.aef0db9b684d219b79c1da7cbd95f0d6 # or whatever the real entity_id for the binary sensor is
    to: 'on'    
action:
  - if:
      - condition: state
        entity_id: binary_sensor.0c5191974d52b45fd407943b75d2d7d2 # same here
        state: 'on'
    then:
      - service: switch.turn_on
        entity_id: switch.7ab167660a4620e1575494471936f6d7 # same
      - delay:
          seconds: 30
      - if:
          - condition: state
            entity_id: binary_sensor.0c5191974d52b45fd407943b75d2d7d2
            state: 'on'
        then:
          - service: switch.turn_on
            entity_id: switch.7ab167660a4620e1575494471936f6d7 # same
          - delay:
              seconds: 30
          - if:
              - condition: state
                entity_id: binary_sensor.0c5191974d52b45fd407943b75d2d7d2 #same
                state: 'off'
            then:
              - service: switch.turn_on
                entity_id: switch.2ce7a1c1043e1234dee2748de941fa9f #same
              - service: switch.turn_on
                entity_id: switch.defefbccc9bfa91ba0545e27a3ab8637 # same
        else:
          - service: switch.turn_on
            entity_id: switch.2ce7a1c1043e1234dee2748de941fa9f
          - service: switch.turn_on
            entity_id: switch.defefbccc9bfa91ba0545e27a3ab8637
    else:
      - service:switch.turn_on
        entity_id: switch.2ce7a1c1043e1234dee2748de941fa9f
      - service: switch.turn_on
        entity_id: switch.defefbccc9bfa91ba0545e27a3ab8637
mode: single

if those entity_id’s are correct the first thing I would do is to change them to something more human readable. you should be able to do that thru the UI (at least for most/many integrations).

TBH, I’m not sure how you could use a wait instead of a delay unless the delay of 30 seconds might not be long enough to allow the binary sensor to come on before a retry of the “rollup” switch.

to me it looks like the code above is exactly as you have in your flow chart.

But if the goal is to repeat the switch turn on action until the binary sensor turns off I think I would just use a “repeat-until” action. And you can then use an repeat index to stop the run after a certain number of tries.

I think based on your flow chart this will do it:

alias: Gen Start
trigger:
  - platform: state
    entity_id: binary_sensor.aef0db9b684d219b79c1da7cbd95f0d6 # or whatever the real entity_id for the binary sensor is
    to: 'on'    
action:
  - choose:
      - condition: state
        entity_id: binary_sensor.0c5191974d52b45fd407943b75d2d7d2 # same here
        state: 'on'
    sequence:
      - repeat:
          sequence:
            - service: switch.turn_on
              entity_id: switch.7ab167660a4620e1575494471936f6d7 # same
            - delay:
                seconds: 30
        until:
          condition: or
          conditions:
            - condition: state
              entity_id: binary_sensor.0c5191974d52b45fd407943b75d2d7d2 # same
              state: 'off'
            - "{{ repeat.index == 3 }}"
      - if:
          - condition: state
            entity_id: binary_sensor.0c5191974d52b45fd407943b75d2d7d2 #same
            state: 'on'
        then:
          - some_other_thing
    default:
      - service:switch.turn_on
        entity_id: switch.2ce7a1c1043e1234dee2748de941fa9f # 
      - service: switch.turn_on
        entity_id: switch.defefbccc9bfa91ba0545e27a3ab8637 #
mode: single

it turns on the switch - waits 30 seconds and if the binary sensor is still on then again turns on the switch - waits 30 seconds - … until it repeats the loop 3 times. it will then exit the repeat loop and do the thing in the “if” if the binary sensor is still on.

I think…

But the other thing I noticed is that nothing in your flow turns the rollup switch back off again. So the switch turn on won’t do anything since the switch is already on. you’ll need to deal with that if it’s an issue.

THANK YOU! Super helpful! Im cheating with the door switch. for a momentary. I turn the switch on and I let the Shelly SW turn it off after a shelly timer times out (2s I think). I will clear up the device_IDs. For some reason the developers tools->states tab only shows a few shelly entities, (reboot, firmware upd, etc) the switch and binary_inputs are no where to be found; but if switch to using states/conditions and services it should be a null issue, correct?

there’s nothing you can do about the device_id’s. those are generated by the system. You only have control over the entity_id’s and friendly names of the entities.

the dev tools->states page should show every entity associated with your system whether it is part of a device or not. Not all integrations create devices but almost all of them create entities. The only ones that don’t are the integrations that only create services (shell scripts, notifiers, etc).

if they don’t show up in the states list then they won’t be available as entities for use in triggers, actions or conditions.

But I would think that everything that shows up if you go to settings->devices & services->integrations-> select your integration (shelly in your case) and select the shelly device that you want to see. everything there that’s provided by the device should have an entity in the states page too.

Also just be clear, not all integrations will show up in the integrations listing either. only the ones configured by the UI. But even the ones not configured in the UI (therefore not listed in the UI configuration list) will still provide entities but not not devices.

hopefully I’m not confusing you more.

i think i’m understanding you and your explaining things really helps (not confusing me more!). In the example you wrote for me, you used the entity_id that you pulled from my GUI version. no where in any lists do entity_ids show up matching the ones that the GUI generates.

The REAL question is: In line 4 of the code you wrote: would it be equal to say:

  1. entity_id: binary_sensor.aef0db9b684d219b79c1da7cbd95f0d6
    or
  2. entity_id: binary_sensor.shellypi4_gen_rollup_input_0_input

Noting that the name in 2) is what I entered in the GUI which generated the name in 1)

Does that entity id in 2 show up in your states list?

Your first flowchart (Turn-on Flow), employs a WAIT block that waits for Input_0 to turn off for up to a maximum timeout of 30 seconds.

Your automation doesn’t appear to do that but simply employs a fixed delay of 30 seconds.

If you want the automation to wait then it should employ either a wait_template or wait_for_trigger.

What exactly are you expecting?

wait_template waits for its template (the one you provide) to evaluate to true.

This is my interpretation of Turn-on Flow. Let me know if I misinterpreted it.

alias: turn on flow
trigger:
  - platform: state
    entity_id: binary_sensor.rollup_input_2
    from: 'off'
    to: 'on'
condition: []
action:
  - if: "{{ is_state('binary_sensor.rollup_input_0', 'on') }}"
    then:
      - service: switch.turn_on
        target:
           entity_id: switch.rollup_switch_0
      - wait_template: "{{ is_state('binary_sensor.rollup_input_0', 'off') }}"
        timeout:
          seconds: 30
        continue_on_timeout: true
  - service: switch.turn_on
    target:
       entity_id:
         - switch.fan_switch_0
         - switch.run_switch_0

Yes it does show up on the Dev_Tools>States list

Your code is on the right track. The last 5 lines would be an ‘else’
And when we fall out of the wait we need to loop back to the if 1 more time. Also if we loop back to the if because we passed the wait test, it will not hurt to run the if test again (easier coding and almost clean, technically we are double testing)

THANK YOU for your help

According to your flowchart, whether Input_0 is on or off, ultimately either case turns off the two switches.

“Fall out” under what conditions? If the wait_template evaluated to true or if it never evaluated to true and timed out?

  1. If the wait_template evaluates to true, what’s the point of looping back?
  2. If the wait_template times out then I sort of see the point of looping back but it’s flawed because, if things go really wrong, it has the potential to loop endlessly.

You are correct. there is a missing decision on my flow chart. I need to add a block to the WAIT-30sec timeout loop back. If it has looped back twice without input_0 going to OFF, then I need to exit with out turning the last too switches in the flow chart and send a notification of FAILURE TO START. (the reason for up to 2 loop backs is: If the door has been manually stopped in the middle, 1st activation will either finish OPENING or CLOSING depending of travel direction when STOPPED. it that 1st activation closed it, it will need a second activation to fully open.

With what you and Finity have shown me, let me write a hand code. (i do have the “brute force” GUI code working 100% as desired, but is messy and hard to edit as more things come up (room temp, gen temp, CO levels in room, etc, etc).

FYI, I’m off grid with a 3 phase solar machine shop and race car fab (all personal rec, no business). I’ll throw in a few pics. The Backup gen is 70KW and the array is 80KW with 200A 3 phase 220v output. All self installed :blush:


If the “looping back” involves turning on the switch again then perhaps you should use a repeat - until.

Either update your flowchart or post it as pseudo code and I’ll convert it into an automation.