Help With Variables, Calculations for Automation

I’m trying to create an automation to set the watering level for my sprinklers using a soil moisture sensor as the input, and the watering level in Opensprinkler as the output.

The slightly tricky part for me is that I need to calculate the “inverse” percentage as set that as the watering level, using 50% as the break even point. For example

soil moisture: 50%
watering level: 50%

soil moisture 30%
watering level: 70%

soil moisture 70%
watering level 30%

I was thinking about having the trigger run once per day at midnight, and then in actions defining a variable that pulls the soil moisture level, then performs the percentage calculation, then when calling the Action to set the watering level specify the variable.

alias: Sprinkler Set Watering Level
description: ""
triggers:
  - trigger: time
    at: "00:30:00"
conditions: []
actions:
  - variables:
      soil_moisture_level: {{ sensor.gw2000b_soil_moisture_2 }} some-calulation-here
  - action: opensprinkler.set_water_level
    metadata: {}
    data:
      water_level: {{ soil_moisture_level }}
mode: single

Hi virtualbitzz,

I have done this using the Compensation Integration. Here’s my notes and YT Video…
Zigbee Hose Valve Automation Home Assistant | What Are We Fixing Today?.
and a link to the integration
Compensation - Home Assistant.

alias: Sprinkler Set Watering Level
description: ""
triggers:
  - trigger: time
    at: "00:30:00"
conditions: []
actions:
  - variables:
      soil_moisture_level: "{{ 100 - states('sensor.gw2000b_soil_moisture_2') | float(0) }}"
  - action: opensprinkler.set_water_level
    metadata: {}
    data:
      water_level: "{{ soil_moisture_level }}"
mode: single

I actually ended up using the compensation integration as @Sir_Goodenough suggested. The last part I’m stuck on is getting the action to accept the template.

action: opensprinkler.set_water_level
metadata: {}
data:
  water_level: {{ (states("sensor.compensation_sensor_gw2000b_soil_moisture_2") | int }}
target:
  entity_id: switch.opensprinkler_enabled

If I run the template through the template editor is produces an integer with the expected value, however I get this error when running the action

Error running action
expected int for dictionary value @ data['water_level']. Got None

Quotes around the template…

I got it working, thanks!

action: opensprinkler.set_water_level
metadata: {}
data:
  water_level: "{{ states('sensor.compensation_sensor_gw2000b_soil_moisture_2') | float(0) }}"
target:
  entity_id: switch.opensprinkler_enabled
1 Like