Trying to add a fertigation dosage slider and schedule

Home Assistant automation projects, questions, etc. go here.
Post Reply
SargeOG
LED-Curious
LED-Curious
Reactions:
Posts: 6
Joined: Tue Feb 02, 2021 1:05 pm

Hey guys, so what I'm trying to do is to add a fertigation dosage slider that is in liters. I measured 1 liter to be 14 seconds. I managed to make it covert liters to seconds in node red with someone's help.
Now when I slide the liters around I get a msg.payload with the exact converted seconds. Which is awesome as it is.

But now I don't really know how to add a schedule to open at set hours and be open for those exact amount of seconds. Any help with this would be nice, to get me going.

I have to confess I'm literally a noob but I love doing this automation stuff to my plants. It really dragged me in the whole :D
00schteven
LED-Curious
LED-Curious
Reactions:
Posts: 23
Joined: Thu Dec 31, 2020 10:39 pm

@stevetheganjafarmer
SargeOG
LED-Curious
LED-Curious
Reactions:
Posts: 6
Joined: Tue Feb 02, 2021 1:05 pm

Yeah I already got a helper (input.number) made to be a slider in liters, attached in node red to a function that converts those liters to seconds. Now I just need to attach that input in seconds in time spent open for the valve when it's scheduled to open.

Any input would be great!
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Sounds like you just need a block in node red to read the state of the slider, then do a switch.turn_on to your pump entity with a delay for however long your slider value is, then a switch.turn_off, right? Can you paste your node red flow here?
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!
SargeOG
LED-Curious
LED-Curious
Reactions:
Posts: 6
Joined: Tue Feb 02, 2021 1:05 pm

I have no real idea of what I am doing really, but here you go... Thank you for answering!

[{"id":"6faa0663.1b2b08","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"835eb1c9.186c28","type":"function","z":"6faa0663.1b2b08","name":"Math","func":"newmsg = {};\nvar liters = msg.payload;\nvar l1 = liters * 14; \nnewmsg.payload = l1;\nreturn newmsg;","outputs":1,"noerr":0,"initialize":"","finalize":"","x":310,"y":120,"wires":[["3c809786.0219a","74cff798.b338c8"]]},{"id":"3c809786.0219a","type":"debug","z":"6faa0663.1b2b08","name":"Liters","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":510,"y":120,"wires":[]},{"id":"ea95d522.bd0628","type":"server-state-changed","z":"6faa0663.1b2b08","name":"Liters","server":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":"Alarm 1"},{"property":"icon","value":"mdi:fire"}],"entityidfilter":"input_number.dozaj","entityidfiltertype":"exact","outputinitially":false,"state_type":"num","haltifstate":"","halt_if_type":"num","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"x":130,"y":120,"wires":[["835eb1c9.186c28"]]},{"id":"5a5292fd.7d7bdc","type":"delay","z":"6faa0663.1b2b08","name":"","pauseType":"delayv","timeout":"0","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":720,"y":240,"wires":[["f9b8dfe5.6734c"]]},{"id":"74cff798.b338c8","type":"change","z":"6faa0663.1b2b08","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"delay","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":510,"y":240,"wires":[["5a5292fd.7d7bdc"]]},{"id":"f9b8dfe5.6734c","type":"debug","z":"6faa0663.1b2b08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":930,"y":240,"wires":[]}]
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

Here you go:

I have 3 date_time inputs in this flow but you can add more or remove some, or whatever you like. You will need to go to helpers and add 3 date_time inputs as 'time only' inputs, and put these on your dashboard. Go into each one of the state_changed blocks in the Node red flow and point the entity_id to the 3 inputs you created. Go into both of the call_service blocks at the far right and set the switch.entityID to the entity ID of the power switch you will be controlling to run your pump.

flow.png

Now go into the function node and edit the green strings so they have these same entity ID's as you just entered for the state_changed blocks:

Function.png

And you should be set.

Code: Select all

[{"id":"6faa0663.1b2b08","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"5a5292fd.7d7bdc","type":"delay","z":"6faa0663.1b2b08","name":"","pauseType":"delayv","timeout":"0","timeoutUnits":"seconds","rate":"1","nbRateUnits":"1","rateUnits":"second","randomFirst":"1","randomLast":"5","randomUnits":"seconds","drop":false,"x":1080,"y":280,"wires":[["2e9a3423.86a44c"]]},{"id":"de430bbb.9b20d","type":"api-call-service","z":"6faa0663.1b2b08","name":"","server":"","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_on","entityId":"switch.YOUR_PUMP_SWITCH_ENTITY_ID","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1290,"y":200,"wires":[[]]},{"id":"2e9a3423.86a44c","type":"api-call-service","z":"6faa0663.1b2b08","name":"","server":"","version":1,"debugenabled":false,"service_domain":"switch","service":"turn_off","entityId":"switch.YOUR_PUMP_SWITCH_ENTITY_ID","data":"","dataType":"json","mergecontext":"","output_location":"","output_location_type":"none","mustacheAltTags":false,"x":1290,"y":280,"wires":[[]]},{"id":"72f60bde.bf77a4","type":"inject","z":"6faa0663.1b2b08","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"1","crontab":"","once":false,"onceDelay":0.1,"topic":"currentTime","payload":"$substring($now(),11,8)","payloadType":"jsonata","x":110,"y":120,"wires":[["9d1ce8e9.0daed"]]},{"id":"e94a6761.f6d96","type":"server-state-changed","z":"6faa0663.1b2b08","name":"","server":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_datetime.YOUR_FERTIGATION_DATETIME1","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":280,"y":300,"wires":[["9d1ce8e9.0daed"]]},{"id":"698a8d9.a61bd74","type":"function","z":"6faa0663.1b2b08","name":"","func":"// SET THIS CONSTANT TO HOWEVER LONG IT TAKES TO PUMP 1 LITER, IN SECONDS\nconst secondsPerLiter = 14; \n\nvar currentTime  = msg.payload[\"currentTime\"];\n\n//CHANGE THE STRINGS BELOW IN GREEN TO THE ENTITY IDs OF YOUR INPUT_DATE TIMES \nvar fertTime1    = msg.payload[\"input_datetime.YOUR_FERTIGATION_DATETIME1\"];\nvar fertTime2    = msg.payload[\"input_datetime.YOUR_FERTIGATION_DATETIME2\"];\nvar fertTime3    = msg.payload[\"input_datetime.YOUR_FERTIGATION_DATETIME3\"];\n\n//THIS CONVERTS YOUR 'LITERS' INPUT NUMBER TO PUMP TIME IN MILLISECONDS \nvar fertDuration = (msg.payload[\"input_number.dozaj\"] * secondsPerLiter) *1000;\n\n//THIS RETURNS THE DELAY TIME TO THE VARIABLE DELAY BLOCK\nmsg.delay = fertDuration;\n\n//IF ANY OF YOUR INPUT_DATETIMES MATCH THE CURRENT TIME, TURN ON THE PUMP AND THEN TURN OFF AFTER DELAY EXPIRES\nif(fertTime1 == currentTime || fertTime2 == currentTime || fertTime3 == currentTime)\n    {\n        return msg;\n    }\n    \n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":900,"y":220,"wires":[["5a5292fd.7d7bdc","de430bbb.9b20d"]]},{"id":"9d1ce8e9.0daed","type":"join","z":"6faa0663.1b2b08","name":"","mode":"custom","build":"object","property":"payload","propertyType":"msg","key":"topic","joiner":"\\n","joinerType":"str","accumulate":true,"timeout":"","count":"1","reduceRight":false,"reduceExp":"","reduceInit":"","reduceInitType":"","reduceFixup":"","x":730,"y":220,"wires":[["4f79b5b5.5b159c","698a8d9.a61bd74"]]},{"id":"6b6110ee.c5c5e8","type":"server-state-changed","z":"6faa0663.1b2b08","name":"","server":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_datetime.YOUR_FERTIGATION_DATETIME2","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":280,"y":380,"wires":[["9d1ce8e9.0daed"]]},{"id":"3cdca92.4db64d6","type":"server-state-changed","z":"6faa0663.1b2b08","name":"","server":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":""},{"property":"icon","value":""}],"entityidfilter":"input_datetime.YOUR_FERTIGATION_DATETIME3","entityidfiltertype":"exact","outputinitially":true,"state_type":"str","haltifstate":"","halt_if_type":"str","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":0,"forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":false,"ignoreCurrentStateUnavailable":false,"x":280,"y":460,"wires":[["9d1ce8e9.0daed"]]},{"id":"4f79b5b5.5b159c","type":"debug","z":"6faa0663.1b2b08","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":910,"y":320,"wires":[]},{"id":"17ee2fe5.c30eb8","type":"server-state-changed","z":"6faa0663.1b2b08","name":"Liters","server":"","version":1,"exposeToHomeAssistant":false,"haConfig":[{"property":"name","value":"Alarm 1"},{"property":"icon","value":"mdi:fire"}],"entityidfilter":"input_number.dozaj","entityidfiltertype":"exact","outputinitially":true,"state_type":"num","haltifstate":"","halt_if_type":"num","halt_if_compare":"is","outputs":1,"output_only_on_state_change":true,"for":"","forType":"num","forUnits":"minutes","ignorePrevStateNull":false,"ignorePrevStateUnknown":false,"ignorePrevStateUnavailable":false,"ignoreCurrentStateUnknown":true,"ignoreCurrentStateUnavailable":true,"x":90,"y":220,"wires":[["9d1ce8e9.0daed"]]},{"id":"62bf8387.dc9904","type":"comment","z":"6faa0663.1b2b08","name":"Send the current time every second","info":"","x":180,"y":80,"wires":[]}]
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!
dreadheaded
LED-Curious
LED-Curious
Reactions:
Posts: 2
Joined: Thu Feb 11, 2021 1:13 pm

I came across this code and have been trying to adapt it to my requirements, I would like to add a slider to set the amount of fertigations, I added the slider and tried various changes to the function block trying to get it to work but I cant seem to work it out.
User avatar
LEDG
Site Admin
Reactions:
Posts: 1599
Joined: Sun Jun 04, 2017 8:15 pm

I'm doing mine with a custom card to allow me to show and hide each of my 5 input_datetime cards based on what my frequency slider is set to. The code in the custom card is:

Code: Select all

cards:
  - entities:
      - input_number.fertigation_freq
      - input_number.fertigation_duration
    show_header_toggle: false
    title: Fertigations
    type: entities
  - card:
      entities:
        - input_datetime.fert_1_time_4x4
      type: entities
    conditions:
      - entity: binary_sensor.fert_1
        state: 'on'
    type: conditional
  - card:
      entities:
        - input_datetime.fert_2_time_4x4
      type: entities
    conditions:
      - entity: binary_sensor.fert_2
        state: 'on'
    type: conditional
  - card:
      entities:
        - input_datetime.fert_3_time_4x4
      type: entities
    conditions:
      - entity: binary_sensor.fert_3
        state: 'on'
    type: conditional
  - card:
      entities:
        - input_datetime.fert_4_time_4x4
      type: entities
    conditions:
      - entity: binary_sensor.fert_4
        state: 'on'
    type: conditional
  - card:
      entities:
        - input_datetime.fert_5_time_4x4
      type: entities
    conditions:
      - entity: binary_sensor.fert_5
        state: 'on'
    type: conditional
type: vertical-stack

In my configuration.yaml file, I have input_numbers for fertigation frequency (how many fertigations per light cycle) and fertigation duration ( my "pump on" time, in minutes). I have 5 input_datetimes declared to store the "on" time for each fertigation, and 5 binary sensors declared to track the state of the fertigation frequency slider and make the cards appear and disappear in the custom card I pasted above.

Code: Select all

input_number:
# FERTIGATION FREQ/DURATION    
  fertigation_duration:
    name: Duration
    min: .5
    max: 10
    step: .5
    icon: mdi:timer
    unit_of_measurement: Minutes
  fertigation_freq:
    name: Frequency
    min: 0
    max: 5
    step: 1
    icon: mdi:pound-box
    unit_of_measurement: Fertigations

Code: Select all

input_datetime:
  lights_on_time_4x4:
    name: 4x4 Lights On Time
    has_date: false
    has_time: true
  lights_off_time_4x4:
    name: 4x4 Lights Off Time
    has_date: false
    has_time: true
  fert_1_time_4x4:
    name: Fertigation 1 Start Time
    has_date: false
    has_time: true
  fert_2_time_4x4:
    name: Fertigation 2 Start Time
    has_date: false
    has_time: true
  fert_3_time_4x4:
    name: Fertigation 3 Start Time
    has_date: false
    has_time: true
  fert_4_time_4x4:
    name: Fertigation 4 Start Time
    has_date: false
    has_time: true
  fert_5_time_4x4:
    name: Fertigation 5 Start Time
    has_date: false
    has_time: true

Code: Select all

################################ BINARY SENSORS ################################

# FERTIGATION BINARY SENSORS
# These sensors go high/low based on the state of the fertigation frequency
# input number. They are used to control visibility of the fertigation time 
# cards found in the schedule tab of the dashboard. 
binary_sensor:
  - platform: template
    sensors:
      fert_1:
        friendly_name: "One Fert"
        value_template: >-
          {{ states("input_number.fertigation_freq") | float >= 1.0 }}
  - platform: template
    sensors:
      fert_2:
        friendly_name: "Two Ferts"
        value_template: >-
          {{ states("input_number.fertigation_freq") | float >= 2.0 }}
  - platform: template
    sensors:
      fert_3:
        friendly_name: "Three Ferts"
        value_template: >-
          {{ states("input_number.fertigation_freq") | float >= 3.0 }}
  - platform: template
    sensors:
      fert_4:
        friendly_name: "Four Ferts"
        value_template: >-
          {{ states("input_number.fertigation_freq") | float >= 4.0 }}
  - platform: template
    sensors:
      fert_5:
        friendly_name: "Five Ferts"
        value_template: >-
          {{ states("input_number.fertigation_freq") | float >= 5.0 }}
I then have 5 automations built in my automations.yaml file that check the system time and if it matches any of the stored fertigation times, it triggers a script to turn on the pump for the amount of time set with the fertigation duration slider, then turn it off:

The automations:

Code: Select all

- id: 12autofert1
  alias: Fertigation 1
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == states(''input_datetime.fert_1_time_4x4'')[0:5]
      }}'
  condition:
  - condition: state
    entity_id: binary_sensor.fert_1
    state: 'on'
  action:
  - data: {}
    entity_id: script.1fertigate1
    service: script.turn_on
- id: 12autofert2
  alias: Fertigation 2
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == states(''input_datetime.fert_2_time_4x4'')[0:5]
      }}'
  condition:
  - condition: state
    entity_id: binary_sensor.fert_2
    state: 'on'
  action:
  - data: {}
    entity_id: script.1fertigate2
    service: script.turn_on
- id: 12autofert3
  alias: Fertigation 3
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == states(''input_datetime.fert_3_time_4x4'')[0:5]
      }}'
  condition:
  - condition: state
    entity_id: binary_sensor.fert_3
    state: 'on'
  action:
  - data: {}
    entity_id: script.1fertigate3
    service: script.turn_on
- id: 12autofert4
  alias: Fertigation 4
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == states(''input_datetime.fert_4_time_4x4'')[0:5]
      }}'
  condition:
  - condition: state
    entity_id: binary_sensor.fert_4
    state: 'on'
  action:
  - data: {}
    entity_id: script.1fertigate4
    service: script.turn_on
- id: 12autofert5
  alias: Fertigation 5
  description: ''
  trigger:
  - platform: template
    value_template: '{{ states(''sensor.time'') == states(''input_datetime.fert_5_time_4x4'')[0:5]
      }}'
  condition:
  - condition: state
    entity_id: binary_sensor.fert_5
    state: 'on'
  action:
  - data: {}
    entity_id: script.1fertigate5
    service: script.turn_on
The scripts in my scripts.yaml file:

Code: Select all

1fertigate1:
  alias: Fertigation Event 1
  sequence:
  - entity_id: switch.relay_4ch_1_2                     # Turn on the fertigation pump for the duration set by the input number on UI, then turn off.
    service: switch.turn_on
  - delay: '{{ states(''input_number.fertigation_duration'') | multiply(60) | timestamp_custom(''%H:%M:%S'',False)
      }}'
  - entity_id: switch.relay_4ch_1_2
    service: switch.turn_off
1fertigate2:
  alias: Fertigation Event 2
  sequence:
  - entity_id: switch.relay_4ch_1_2
    service: switch.turn_on
  - delay: '{{ states(''input_number.fertigation_duration'') | multiply(60) | timestamp_custom(''%H:%M:%S'',False)
      }}'
  - entity_id: switch.relay_4ch_1_2
    service: switch.turn_off
1fertigate3:
  alias: Fertigation Event 3
  sequence:
  - entity_id: switch.relay_4ch_1_2
    service: switch.turn_on
  - delay: '{{ states(''input_number.fertigation_duration'') | multiply(60) | timestamp_custom(''%H:%M:%S'',False)
      }}'
  - entity_id: switch.relay_4ch_1_2
    service: switch.turn_off
1fertigate4:
  alias: Fertigation Event 4
  sequence:
  - entity_id: switch.relay_4ch_1_2
    service: switch.turn_on
  - delay: '{{ states(''input_number.fertigation_duration'') | multiply(60) | timestamp_custom(''%H:%M:%S'',False)
      }}'
  - entity_id: switch.relay_4ch_1_2
    service: switch.turn_off
1fertigate5:
  alias: Fertigation Event 5
  sequence:
  - entity_id: switch.relay_4ch_1_2
    service: switch.turn_on
  - delay: '{{ states(''input_number.fertigation_duration'') | multiply(60) | timestamp_custom(''%H:%M:%S'',False)
      }}'
  - entity_id: switch.relay_4ch_1_2
    service: switch.turn_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!
dreadheaded
LED-Curious
LED-Curious
Reactions:
Posts: 2
Joined: Thu Feb 11, 2021 1:13 pm

Thank you this works great
SargeOG
LED-Curious
LED-Curious
Reactions:
Posts: 6
Joined: Tue Feb 02, 2021 1:05 pm

OMG! Thanks so much @LEDG !!! It worked like a charm! Thanks so much, I'm so glad it's working now! :D

But I stumbled upon 2 new issues. It's the current time injection, apparently it gives me the time by 2 hours earlier than my actual time. Example: Local time 07:25, injection time: 05:25, what could cause this?

Also the other issue is there seems to be a short activation of the pump whenever I deploy from NodeRed, it's very short, maybe a few miliseconds, then it closes. But just a few hours ago I deployed and apparently the pump never closed back, so it pumped everything from the reservoir and I noticed a bit too late as it made a mess and overflowed the runoff tank :( bummer.

I don't know why the pump activates on deploy, I didn't test on HA restart, could happen in that case too, but IDK. Is there a way to stop it from activating like this?

Thanks so much for all your effort, you inspired me greatly to learn automation and it works awesome with my plants especially!
Post Reply