Logic processing and other issues

I like Home Assistant.
I like it a lot.
But I am slowly finding myself start wondering if it can handle anything with any slightly complicated logic.

I came here after trying out OpenHAB and to be fair I liked that a lot less but in the short time I ‘evaluated’ it I felt it was better documented so less of an ordeal to learn.

I am however finding some quite fundamental things apparently missing from the functionality of HA to be frustrating. Such as the lack of the ability to have a dynamically defined delay and now what appears to be a lack of any real logic programming. Specifically. I have re-worked my project (which I don’t consider to be particularly complex) several times to try and get around the foibles of HA and today I thought I had cracked it, but when it didn’t work I found this [Calling scripts from a script] which although only two posts from Jan '17 and Jun '17 seems to preclude scripts being used as a useful logic processing feature. I have not seen that documented anywhere and indeed, do these two posts even make it true?

And yes, I am new here so I could be wrong about all this - I would love it if I was and apologise in advance - but the main issue I am having is not how to do things, i am happy to read, learn and try, but that the most fundamental ways in which HA works do not seem to be well documented, or if they are I can’t find them.

I am not complaining as such, I know this is open source with all that that entails, I have nothing but admiration for, and am very grateful indeed to, everyone who has contributed in any way to this project. I will also continue working with it because as I said at the beginning I like it a lot. I just wish it were easier to at least find out what HA is capable of so that at least I don’t spend my time chasing solutions only to find that they are simply not possible!

This post is born out of frustration, not anger or criticism - there are some people here doing some very, very clever stuff - I just wish I knew where to find the resources (if they exist) to enable me to learn more about what I am able to achieve. Learning how to do things is the easy bit. First you need to know what is possible.


In the unlikely event that after having said all that anyone wants to tell me (with some pseudo code to suit my situation?) I am happy to share what I am trying to do :wink::roll_eyes::grin:

What are you trying to do? HA is very flexible, I’m sure it’s possible to do what you want.

1 Like

Thanks for asking. I was largely inspired by this [Garden Irrigation] but to be honest my solution has some very different needs.

I will be as concise as possible:
One garden tap supplies a mechanical water distributor with six outlets. Two of those outlets must be used, the other four are optional. Stopping the water supply advances the distributor to the next outlet (zone)

So, I need to open and close a single valve (using a sonoff). It opens for zone 1 for a user defined period of time and then closes (for a short time, say 15 seconds) to allow the distributor to advance, before opening again for zone 2 for another user defined period of time before again closing to advance to the next zone. Then, for any other optional zone (3 to 6) the process repeats.

The user defines the start time of the whole operation, the duration for each zone to be open and which zones over and above 1 and 2 are in operation. All variables (zones used and their durations) are user configurable.

I thinks that covers it. There are other situations / options that I want to include (for example unattended daily or alternate daily runs, on demand runs), but I think they might become almost trivial to implement (ha! ha!) once the above is in place.


Here are the notes (pseudo code) I made when I thought scripts were the answer:

Automation: start irrigation
	set and start timer with zone 1 duration
	call run pump script

	start and start timer with zone 2 duration	
	call run pump script

	call zone3 script
	call zone4 script
	call zone5 script
	call zone6 script

script: zone3 (repeat for zones 4, 5 and 6)
  condition: zone3 connected
	set and start timer with zone 3 duration
	call run pump script

script: run pump
	start pump
	wait until timer finished (don't forget a timeout)
	stop pump
	delay 15 seconds

It is of course quite possible that this is simple and if so feel free to let me know that I should have been able to work it out!

I wonder if part of the problem is wanting to modularise the code. I find yaml files get very long very quickly and I worry about future enhancements and maintenance.

I’m sorry this is so long but thanks for your interest.


For completeness this is the Gardena product.

https://www.gardena.com/uk/products/watering/water-controls/water-distributor-automatic/966749301/

Have you tried any automation/script code on this yet? I assume yes since you said it didn’t work.

It might be a good start to post the code you have now to see if you are on the right track and/or where the failure lies.

remember to properly format the code for display by selecting the code block and using the </> symbol in the editor window.

@finity yes I have tried loads of code on this. Several iterations in fact.
I am now back to trying to follow the version based on the above pseudo code as I have realised that in order to have scripts run serially rather than in parallel I need to wait after calling a script for it to finish. Not exactly elegant but should work fine.
My current code is here but it is still not working because for some reason HA reports:

Unable to find service timer/start
(I think I’ll open a separate thread for that if I can’t find it anywhere else)

Also both of my test persistent messages in the automation work but the one in the script doesn’t which implies the script is never actually running?

Edit: I restarted HA and now the script does run - I get all three messages but it still reports

Unable to find service timer/start

so it seems to be making progress except that some logic somewhere is off as I get

Script script.open_valve already running.

and the switch never turns off.
I’ll investigate! :slight_smile:

Edit (again): (thankfully before anyone has wasted any time to investigate and reply) err… rookie error. I forgot to put an include in my configuration.yaml for timers. :scream: I am surprised the config checked ok.

This is my automation:

- alias: Run cycle once immediately
  trigger:
    platform: state
    entity_id: input_boolean.run_cycle_once_immediately
    to: 'on'
  action:
    # ZONE 1
    # Start timer for zone 1 duration
    - service: timer.start
      entity_id: timer.irrigation_duration
      data_template:
        duration: "00:{{ states('input_number.zone1_duration')|int }}:00"

    - service: persistent_notification.create
      data_template:
        message: 'zone 1 test message'

    # Run for duration
    - service: homeassistant.turn_on
      entity_id: script.open_valve
    # wait for script to finish
    - wait_template: "{{ states('script.open_valve') == 'off'}}"
      timeout: '00:00:10'

    # ZONE 2
    # Start timer for zone 2 duration
    - service: timer.start
      entity_id: timer.irrigation_duration
      data_template:
        duration: "00:{{ states('input_number.zone2_duration')|int }}:00"

    - service: persistent_notification.create
      data_template:
        message: 'zone 2 test message'

    # Run for duration
    - service: homeassistant.turn_on
      entity_id: script.open_valve
    # wait for script to finish
    - wait_template: "{{ states('script.open_valve') == 'off'}}"
      timeout: '00:00:10'  

    # ZONE 3
    - service: homeassistant.turn_on
      entity_id: script.irrigate_zone3
    # wait for script to finish
    - wait_template: "{{ states('script.irrigate_zone3') == 'off'}}"
      timeout: '00:00:10'

    #- zone4
    #- zone5
    #- zone6

Here are my scripts

open_valve:
  alias: "Open valve"
  sequence:
    - service: persistent_notification.create
      data_template:
        message: 'In script open_valve'
    # Open valve    
    - service: homeassistant.turn_on
      entity_id: switch.test_sonoff_basic
    # Run for duration of timer
    - wait_template: "{{ states('timer.irrigation_duration') == 'idle' }}"
      timeout: '00:00:10'
    # close valve
    - service: homeassistant.turn_off
      entity_id: switch.test_sonoff_basic
    # Wait 15 seconds to move to next zone
    - delay: '00:00:15'

# Zone 3
irrigate_zone3:
#  - alias: Irrigate zone 3
  sequence:
    - condition: state
      entity_id: input_boolean.zone3_connected
      state: 'on'
    # Start timer for zone 3 duration
    - service: timer.start
      entity_id: timer.irrigation_duration
      data_template:
        duration: "00:{{ states('input_number.zone3_duration')|int }}:00"
    # Run for duration
    - service: script.open_valve
    # wait for script to finish
    - wait_template: "{{ states('script.open_valve') == 'off'}}"
      timeout: '00:00:10'

So…what’s the result now at this point?

working or no?

Actually it is going quite well now. I think I am hoping for too much elegance in my code and I have a strong desire to keep the yaml files down size which I just don’t think is a realistic option. At least certainly not with my current level of knowledge!

(On that note I wonder if it might be nice if it were possible to !include sections ‘at random’)

It’s all looking good at the moment and once it is complete and running and fully tested i might even share the whole project. It is fairly niche being based in a particular product but it should be completely reusable by anyone else because of its niche-ness.

One thing that caused me hours and hours of frustration was the fact that wait_template doesn’t appear to work in a script (it does in an automation). I did eventually stumble across it being documented as an issue on GitHub and I only mention it here now in case anyone searches for it in the future and finds this post.

Thanks for your interest, I really appreciate it especially after I was so hasty to post before spotting my stupid mistake.

Yeah, no problem. I was a real newb just a few months ago (and I still am in many ways) so I know how it feels to be lost just getting started.

Anyway, i’m glad you got it working