Passing a UI value to an automation trigger

Post Reply
User avatar
HughInDevon
Posts: 63
Joined: Wed Jul 31, 2024 4:50 pm

This is - possibly - the last piece of the jigsaw that will complete my Force Discharge automation. With loads of help from this forum - take a bow, Dave, I now have a series of three automations that are designed to export surplus energy from battery to grid as late as possible before the IOG low-tariff period starts.
As a true nerd, I don't like having parameters inside automations that may need changing as this requires editing the automation each time the parameter value is altered. Much better (more satisfying anyway) to have a dashboard setting to amend the value used in the automation.

My only problem is how do I do this? I have experimented with lots of different ideas but without success. This code is as near as I have got:

Code: Select all

alias: Pass value test
description: >-
  Test passing UI value to automation three trigger
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.battery_soc
    below: >-
      {% set myValue=states('input_number.fd_stopatsoc') | float(1) %} {{myValue }}
conditions:
  - condition: state
    entity_id: select.kh10_inverter_work_mode
    state: Force Discharge
actions:
  - action: logbook.log
    metadata: {}
    data:
      name: FD_AutomationThree
      message: Battery below set SOC, work mode back to Feed-in First
  - action: select.select_option
    metadata: {}
    data:
      option: Feed-in First
    target:
      entity_id: select.work_mode
  - action: logbook.log
    metadata: {}
    data:
      name: FD_AutomationThree
      message: |-
        Grid Export at end is    
          {{states('sensor.feed_in_energy_today')}}
      entity_id: sensor.feed_in_energy_today	  
mode: single
If someone can tell me what I am getting wrong I will be most grateful.
Thanks, Hugh
KH10 with 4 off ECS4800 batteries.
20 Jinko 435W panels in 2 strings. 10 on house, south facing, 10 on barn, east facing.
Solar installation active on 27/11/2024. Export MPAN active on 30.12/2024.
Zappi V2 charger and 2 EVs.
Currently on Octopus Intelligent Go tariff.
Home Assistant modbus integration (Now working - ish)
WyndStryke
Posts: 52
Joined: Mon Nov 18, 2024 9:16 pm

Could you use an input_number template helper as the input?
User avatar
HughInDevon
Posts: 63
Joined: Wed Jul 31, 2024 4:50 pm

Hi, Probably - but I don't know how to do that. Any hints?
KH10 with 4 off ECS4800 batteries.
20 Jinko 435W panels in 2 strings. 10 on house, south facing, 10 on barn, east facing.
Solar installation active on 27/11/2024. Export MPAN active on 30.12/2024.
Zappi V2 charger and 2 EVs.
Currently on Octopus Intelligent Go tariff.
Home Assistant modbus integration (Now working - ish)
WyndStryke
Posts: 52
Joined: Mon Nov 18, 2024 9:16 pm

First set up your input_number:

Settings, devices & services, then select the 4th tab (helpers), click 'create helper', scroll down to 'number', click on that, then pick a name, type in the minimum and maximum values, and the step size (for example, for 0.0% to 100.0% in steps of 0.1%, do 0, 100, 0.1, then 'unit of measurement' (if relevant, it might not be).

That gives you an input_number that you can put onto the dashboard, and also modify with a gauge or whatever.

Then in your automation, you can just pick up the value from that as with any other entity.

So here's an example from mine:

I have an input_number, an input_datetime which I use to record the date & time when I hit the top of the SoC range, and an input_boolean to say whether the top SoC has been calibrated recently or not, and a final input_boolean to record whether all calibration has been completed (both top SoC and bottom SoC).

Code: Select all

alias: FoxESS calibrate set top SoC reached
description: ""
triggers:
  - trigger: numeric_state
    entity_id:
      - sensor.battery_soc
    above: input_number.foxess_calibrate_top_soc
conditions:
  - condition: numeric_state
    entity_id: sensor.battery_soc
    above: input_number.foxess_calibrate_top_soc
actions:
  - action: input_datetime.set_datetime
    metadata: {}
    data:
      datetime: "{{ now().strftime('%Y-%m-%d %H:%M:%S') }}"
    target:
      entity_id: input_datetime.foxess_calibrate_top_reached_date
  - action: input_boolean.turn_on
    metadata: {}
    data: {}
    target:
      entity_id: input_boolean.foxess_calibrate_top_reached
  - choose:
      - conditions:
          - condition: state
            entity_id: input_boolean.foxess_calibrate_top_reached
            state: "on"
          - condition: state
            entity_id: automation.foxess_calibrate_set_bottom_soc_reached
            state: "on"
          - condition: state
            entity_id: input_boolean.foxess_calibrate_today
            state: "on"
        sequence:
          - action: input_boolean.turn_off
            metadata: {}
            data: {}
            target:
              entity_id: input_boolean.foxess_calibrate_today
mode: single
There are more automations around this too, for example one to do the same with bottom SoC, another one which runs near midnight to look at how many days since the top or bottom was last calibrated, and decides whether to run a calibration today or not, and obviously my scheduling automations which do the overnight charge etc, and will charge higher if it decides that a top calibration was needed (etc etc).
User avatar
HughInDevon
Posts: 63
Joined: Wed Jul 31, 2024 4:50 pm

Brilliant! I had no idea it was that simple. I have spent hours trying to wrap everything in {{}} or {%%} and all it needs is the entity id. I had already created my input_number helper, it was just that I didn't know how to reference it in the automation. Anyway, it all seems to work now as I want so my thanks for your valuable assistance.
KH10 with 4 off ECS4800 batteries.
20 Jinko 435W panels in 2 strings. 10 on house, south facing, 10 on barn, east facing.
Solar installation active on 27/11/2024. Export MPAN active on 30.12/2024.
Zappi V2 charger and 2 EVs.
Currently on Octopus Intelligent Go tariff.
Home Assistant modbus integration (Now working - ish)
WyndStryke
Posts: 52
Joined: Mon Nov 18, 2024 9:16 pm

I have spent hours trying to wrap everything in {{}} or {%%}
It depends on what you are trying to do. For example, if you wanted to modify the number rather than use it directly. The simple case is just to look at the entity directly

Code: Select all

conditions:
  - condition: numeric_state
    entity_id: sensor.battery_soc
    above: input_number.foxess_calibrate_top_soc
versus the more complex situation where you need to adjust the value in the comparison. Here we are using a 'template conditional' instead of a simple check of the entity, and we are doing that because we want to adjust it by 25 in this scenario.

Code: Select all

                  - condition: template
                    value_template: >-
                      {{ states('sensor.battery_soc') <=
                      (states('input_number.foxess_calibrate_top_soc') | float -
                      25) }}
Post Reply