How to multiply a numeric state by an input number

I’m using the Smart Irrigation integration to automate my yard irrigation. The integration uses weather data to calculate the amount of time to run each irrigation zone. I want to add an adjustment to the automation so that I can set the watering duration to be anywhere from 0-200% of the calculated time. I’m using an input number to set the percentage adjustment.

So, the integration will run each zone based on the state sensor.smart_irrigation_zone15_hourly_adjusted_run_time.

My adjustment entity is input_number.irrigation_duration_adjustment. It has an input range of 0-200. So I need to create a separate entity that is essentially sensor.smart_irrigation_zone15_hourly_adjusted_run_time * (input_number.irrigation_duration_adjustment/100) = New irrigation run time.

Can this be done?

Yes, using a template sensor.

I’m having trouble getting the template syntax correct. I think what I have below is accurate, but when I hit “check configuration” I just get the spinning wheel of death. I’m hesitant to do a restart unless the config checks out.

  - platform: template
    sensors:
      adjusted_time_zone1:
      value_template: '{{ ((states.sensor.smart_irrigation_zone1_daily_adjusted_run_time | float * (states.input_number.irrigation_duration_adjustment / 100))) }}'
      friendly_name: 'Adjusted Irrigation Time_Zone 1'

Does it work in the template dev tool?

  - platform: template
    sensors:
      adjusted_time_zone1:
      value_template: '{{ ((states('sensor.smart_irrigation_zone1_daily_adjusted_run_time') | float * (states('input_number.irrigation_duration_adjustment')) / 100 }}'
      friendly_name: 'Adjusted Irrigation Time_Zone 1'

States are always strings, so they need to be converted to either a float or integer before performing math functions with them. Also, your floats need defaults, there is an indent error, and it is preferred to use the states() format instead of the state object format:

- platform: template
    sensors:
      adjusted_time_zone1:
        value_template: >
          {{ states('sensor.smart_irrigation_zone1_daily_adjusted_run_time') | float(0) *
          (states('input_number.irrigation_duration_adjustment') | float(0) / 100) }}
        friendly_name: 'Adjusted Irrigation Time_Zone 1'

Thanks for the help!

I tried what you posted, @sparkydave but I’m getting a config error

sensor:
- platform: template
    sensors:
      adjusted_time_zone1:
        value_template: >
          {{ states('sensor.smart_irrigation_zone1_daily_adjusted_run_time') | float(0) *
          (states('input_number.irrigation_duration_adjustment') | float(0) / 100) }}
        friendly_name: 'Adjusted Irrigation Time_Zone 1'
Error loading /config/configuration.yaml: mapping values are not allowed here
in "/config/configuration.yaml", line 127, column 11

Line 127 is the ‘sensors:’ (under -platform: template) line

I also want to make sure the order of operations will work correctly. I need to divide input_number.irrigation_duration_adjustment by 100 then multiply the result by sensor.smart_irrigation_zone1_daily_adjusted_run_time

Nevermind. My indentation was off. It works now! Thanks!

Try playing with that template in the dev tool. I’m not great with templates.

In that case you want it to be more like:

sensor:
- platform: template
    sensors:
      adjusted_time_zone1:
        value_template: >
          {{  (states('sensor.smart_irrigation_zone1_daily_adjusted_run_time') | float(0) / 100 ) * 
          (states('input_number.irrigation_duration_adjustment') | float(0))  }}
        friendly_name: 'Adjusted Irrigation Time_Zone 1'