Emulating a Short-Cycle Timer

Home Assistant automation projects, questions, etc. go here.
Gumbo
LED-Curious
LED-Curious
Reactions:
Posts: 10
Joined: Fri Jul 17, 2020 12:15 am

New here and have been following the recent videos by LEDG. I've installed HA on a Raspberry Pi 3, installed the recommended add ons (node-red, sambashare, file editor, Mosquito Broker and ESPhome), and set up remote access.

Since doing that, I've been able to turn on/off an aero pump by adding in some code to the config.yaml file and wiring my Pi into a relay.

Code: Select all

switch:
  - platform: rpi_gpio
    ports:
      12: Pump On
Next, I'd like to be able to create a card in my dashboard that would allow me to set time on and time off of my pump. It'd be somethign like this:

Switch (cycle pump on/off)
Pump time on - (5 sec)
Pump time off - (20 min)

But now I'm stuck. As a beginner, I have NO idea how to get this going in node-red and even less clue how to get it to pull into a card on my dashboard in HA. I've opened node red and tinkered with a few of the nodes to try and get this deployed, but am really at a loss.

Anyone have any video suggestions or anything to get me to the next step(s)?
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Interesting one. It's actually a pretty complex little process since it requires variables for the on/off time and has to loop. I can do this faster without node red since I'm not that comfortable with NR yet...

Here's how I was able to get this to work:

- Under configuration --> helpers, add a toggle and name it "Cycle Pump". The entity ID should become input_boolean.cycle_pump when you create it.

- Again, under helpers, add 2 input numbers. One will be named "Pump On Time" and the other will be "Pump Off Time". Set the min/max values for each and give them a unit of measurement. You can use seconds for the on time and minutes for the off time or whatever you'd like. This is just a label for you to read and doesn't actually change how the input works.

- Add an "Entities" card to your dashboard and add these 3 helpers to it.

- Go to your automations.yaml file and paste this in:

Code: Select all

- id: '1595019292093'
  alias: Cycle Pump GO
  description: ''
  trigger:
  - entity_id: input_boolean.cycle_pump
    platform: state
    to: 'on'
  condition: []
  action:
  - data: {}
    entity_id: script.1595017680358
    service: script.turn_on
- id: '1595019345732'
  alias: Cycle Pump STOP
  description: ''
  trigger:
  - entity_id: input_boolean.cycle_pump
    platform: state
    to: 'off'
  condition: []
  action:
  - data: {}
    entity_id: switch. #PASTE IN THE ENTITY ID OF YOUR SWITCH HERE
    service: switch.turn_off
  - data: {}
    entity_id: script.1595017680358
    service: script.turn_off
  - data: {}
    entity_id: script.1595017404845
    service: script.turn_off
This is going to call a script when you turn your toggle on (which will actually be looping another script), and it's going to stop these scripts and force your pump off when you turn the toggle off.

- Now go to your scripts.yaml and paste this in:

Code: Select all

'1595017404845':
  alias: Cycle Pump On and Off
  sequence:
  - data: {}
    entity_id: switch. #PASTE IN THE ENTITY ID OF YOUR SWITCH HERE
    service: switch.turn_on
  - delay:
      seconds: "{{ states('input_number.pump_on_time') | int }}"  #change the "seconds" at the beginning of this line to minutes or hours or whatever unit  you want your on time input to be... this one matters!
  - data: {}
    entity_id: switch. #PASTE IN THE ENTITY ID OF YOUR SWITCH HERE
    service: switch.turn_off
  - delay:
      seconds: "{{ states('input_number.pump_off_time') | int }}" #change the "seconds" at the beginning of this line if you want your off time input to be in minutes, hours, or whatever
  - data: {}
    service: script.1595017680358
'1595017680358':
  alias: Call Pump Script
  sequence:
  - wait_template: "{{ is_state('script.1595017404845', 'off') }}" #The wait ensures the other script is ready to be called
  - data: {}
    entity_id: script.1595017404845
    service: script.turn_on
The smaller script's only purpose is to call the other script which turns the switches on and off... the big script then calls the little one, which calls the big one... and this repeats until you turn your pump toggle off.

Do a config check and restart if you're all good.
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
Gumbo
LED-Curious
LED-Curious
Reactions:
Posts: 10
Joined: Fri Jul 17, 2020 12:15 am

Dude, thanks for taking the time to help me with this. "Helpers" are a new thing to me and this really helped me understand it a little more.

I'm getting this error when I validate my configuration. Do you know how to troubleshoot it?

Invalid config for [script]: not a valid value for dictionary value @ data['script']['1595017404845']['sequence'][0]['entity_id']. Got 'switch.'. (See /config/configuration.yaml, line 10).

Edit, nevermind! I just read the code and realized I have to enter some names. Doing that now.

Edit again, so I guess I don't know how to get the entity ID of my switch.
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Go to developer tools and the states tab. It should be in there for you to find the entity ID.

Edit: it’ll also be in the entities section of your configuration page
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
Gumbo
LED-Curious
LED-Curious
Reactions:
Posts: 10
Joined: Fri Jul 17, 2020 12:15 am

Awesome! It works! Thanks so much.

Learning many little things along the way.

Another question for you about the rasp pi. I am running off of the GPIO on my Pi. Obviously it seems you and others like to use the esp boards since they're wifi and you can relocate them anywhere while leaving the Pi near the switch. Since I'm waiting to learn the esp stuff and just going to do some simple controls from my Pi, I'd like to move it near my garden to run the pump. Any advice on setting up HA/Pi as wireless?

I found this and may give it a try.

https://community.home-assistant.io/t/w ... -3/93813/4
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Nice!

I just helped somebody set up wireless the other day. Follow lower/upper case as written for everything below:

- Format a USB stick as FAT32 and name the drive CONFIG
- Put a folder in the root of the drive called network.
- Add a new text file to this folder called my-network.txt
- Go here and copy the wireless LAN WPA/PSK example and paste it in to the file you created:
https://github.com/home-assistant/opera ... to-the-lan
- Go here and generate a new UUID and paste it in to the UUID part of the text you pasted:
http://uuidgenerator.net
- Fill out the rest of the text. It should look like mine that I’ve attached.

Edit - Apparently I was mistaken in thinking you had to flash a new copy of home assistant to your SD and boot it at the same time as your USB stick. Sounds like from this point, all you need to do is just power the Pi down, plug the USB stick in and then power the pi up
Attachments
392492EE-4674-4330-A8BC-1ACFB223EDAE.jpeg
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
Gumbo
LED-Curious
LED-Curious
Reactions:
Posts: 10
Joined: Fri Jul 17, 2020 12:15 am

All back to normal and on wifi! Now I can get the aeroponics setup going. I'll have to post some pics soon.
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Gumbo wrote:
Sat Jul 18, 2020 1:30 am
All back to normal and on wifi! Now I can get the aeroponics setup going. I'll have to post some pics soon.
Nicely done.
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
Gumbo
LED-Curious
LED-Curious
Reactions:
Posts: 10
Joined: Fri Jul 17, 2020 12:15 am

Hey LEDG, experienced something with the setup that could use a change. If I switch the "Cycle pump" switch off mid-on cycle, it'll stay on indefinitely. Can we write a check into the script that will allow it to shut off mid cycle?
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

I did account for this but I noticed I forgot to add a comment in my code telling you to replace part of the automation with your switch name like I did for the script.

Did you catch this and edit the automation.yaml part (not the scripts but the automation) to include your switch name or does it say switch.turn_off “switch.outlet3”?

If you did catch this and have already changed it and it still didn’t work, maybe try reordering the automation so it turns the 2 scripts off first and then turns the switch off last. Might even have to add a ~5 second delay after turning the scripts off to make sure they’re off, then turn the switch off.
Want to Support the Site?

Use this Amazon referral link and any purchase you make within 24 hrs will earn LEDgardener a commission at no cost to you!
Post Reply