Set value in action dynamically

I am probably already blind… I don’t see where is it problem.

When I try to set value in action I get error Message malformed: expected float for dictionary value @ data[‘value’]

I have already no idea how do value result more float then float :confused:


      type: set_value
      value: {{ ((states('sensor.allowed_grid_export_limit') | float() * 0.95 | float() / 100 | float()) | round(0, 'floor') | float() * 100 | float()) | float() }}

This sensor is defined like this

- platform: template
  sensors:
    allowed_grid_export_limit:
      unique_id: "svecpetr-23373edefddced7303889e06e2ebc524"
      device_class: power
      friendly_name: "Allowed Grid Export Limit"
      unit_of_measurement: "W"
      value_template: >-
        {{ 8640|float() }}

An finally this is whole declaration

- id: "1685361726670"
  alias: GoodWe - Grid Export - povolení přetoků při kladné ceně za kWh
  description: ""
  trigger:
    - platform: numeric_state
      entity_id: sensor.current_spot_electricity_price
      above: 0
  condition:
    - condition: numeric_state
      entity_id: number.grid_export_limit
      below: sensor.allowed_grid_export_limit
    - condition: template
      value_template:
        "{{ now() - state_attr('automation.goodwe_povoleni_pretoku_pri_kladne_cene_za_kwh',
        'last_triggered') | default(as_datetime(0), true) > timedelta(minutes=5) }}"
  action:
    - device_id: b0a33890a80fbbc0a2cf8fbe4ad46a5f
      domain: number
      entity_id: number.grid_export_limit
      type: set_value
      value: {{ ((states('sensor.allowed_grid_export_limit') | float() * 0.95 | float() / 100 | float()) | round(0, 'floor') | float() * 100 | float()) | float() }}
    - service: notify.mobile_app_sm_s901b
      data:
        message:
          Povoluji přetoky do DS, protože cena výkupu elektřiny je {{ states("sensor.current_spot_electricity_price")|round(2)
          }} Kč/kWh
        title: GoodWe Grid Export Enabled
    - service: notify.seznam_cz
      data:
        title: GoodWe Grid Export Enabled
        message:
          Povoluji přetoky do DS, protože cena výkupu elektřiny je {{ states("sensor.current_spot_electricity_price")|round(2)
          }} Kč/kWh
      enabled: false
  mode: single

You cant use templates in device actions, triggers or conditions. Use the number set value service instead. Also you need to quote single line templates, supply defaults for your float filters and stop applying float filters to numbers that are already numbers. If you use a multi line template you must indicate it with > and not quote it (as I have done for your messages).

action:
  - service: number.set_value
    target:
      entity_id: number.grid_export_limit
    data:
      value: "{{ ( states('sensor.allowed_grid_export_limit') | float(0) * 0.95 / 100 ) | round(0, 'floor') * 100 }}"
    - service: notify.mobile_app_sm_s901b
      data:
        message: >
          Povoluji přetoky do DS, protože cena výkupu elektřiny je {{ states("sensor.current_spot_electricity_price")|round(2)
          }} Kč/kWh
        title: GoodWe Grid Export Enabled
    - service: notify.seznam_cz
      data:
        title: GoodWe Grid Export Enabled
        message: >
          Povoluji přetoky do DS, protože cena výkupu elektřiny je {{ states("sensor.current_spot_electricity_price")|round(2)
          }} Kč/kWh
      enabled: false
  mode: single

Consider never using device actions, triggers or conditions. See: What happens if I have to replace a device - #2 by tom_l

Thanks for help… But it ends with another error

expected float for dictionary value @ data[‘value’]. Got None

I tryied to write to value only number… and it ends still with the same error

obrazek

I am confused of that. I did not understend how to return value to function of setting value :confused:

Please don’t post pictures of text. Post the correctly formatted text.

You quoted a multi line template. After I told you not to. Also you are still applying float filters to numbers when you dont need to. Float filters are used to convert strings (words) to numbers.

All of these should work:
(Just a number)

action:
  - service: number.set_value
    target:
      entity_id: number.grid_export_limit
    data:
      value: 8600

Or this (a number in a quoted single line template):

action:
  - service: number.set_value
    target:
      entity_id: number.grid_export_limit
    data:
      value: "{{ 8600 }}"

Or this (a number in an unquoted multi-line template):

action:
  - service: number.set_value
    target:
      entity_id: number.grid_export_limit
    data:
      value: >
        {{ 8600 }}

Note the differences.

Sorry, my mistake… I see that