Template (or script?) to set values for identically named sensors from input_number

need to set daystart sensors based on power (_actueel) and usage (_totaal) sensors. Way I think I need to do that is setting an input_number netwerk_library_power_daystart: at day change to the value of the sensor.netwerk_libary_actueel. This in its place will be used by a template sensor

      netwerk_library_power_daystart:
        friendly_name: Netwerk library power daystart
        value_template: >
          {{states('input_number.netwerk_library_power_daystart')|float}}

and which I will record and be able to create graphs etc etc.

the same goes for usage_daystart based on sensor.netwerk_library_totaal, creating a template sensor:

      netwerk_library_usage_daystart:
        friendly_name: Netwerk library usage daystart
        value_template: >
          {{states('input_number.netwerk_library_usage_daystart')|float}}

I have 12 sensor/number sets like this, so would love to template the automation…

automation:

  - alias: Set daystart sensors
    trigger:
      platform: state
      entity_id: sensor.date

    condition: []
    action:
      - service: input_number.set_value
        data:
          entity_id: input_number.afzuigkap_zolder_power_daystart
          value: >
            {{states('sensor.afzuigkap_zolder_actueel')}}
      - service: input_number.set_value
        data:
          entity_id: input_number.afzuigkap_zolder_usage_daystart
          value: >
            {{states('sensor.afzuigkap_zolder_totaal')}}

and then…automatically loop all of the sensors, for which I need some assistance…

I’ve created 2 groups for the daystart power, and daystart usage input_numbers, hoping this could facilitate iterating the group and for each sensor.xxx_actueel, set the input_number.xxx_power_daystart, and for each sensor.xxx_totaal, set the input_number.xxx_usage_daystart.

coming to think of that, I might be better of grouping the sensors…?
Hope this makes sense, at least to a level you could help me build this further…

for all ids in expand(‘group.daystart_power’), do:

      - service: input_number.set_value
        variables:
          id : >
          {{entity_id.object_id.split('_power_daystart')[0] }}
        data:
          entity_id: >
            input_number.{{id}}_power_daystart
          value: >
            {{states('sensor.' ~ id ~ '_actueel')}}

to give you the idea…
these templates create the correct entity_id’s for the input_number:

        {%- for s in expand('group.daystart_power') %}
            {% set id = s.object_id.split('_power_daystart')[0] %}
             input_number.{{id}}_power_daystart
        {%- endfor %}

and sensors:

        {%- for s in expand('group.daystart_power') %}
            {% set id = s.object_id.split('_power_daystart')[0] %}
           sensor.{{id}}_actueel 
        {%- endfor %}

or even the value:

        {%- for s in expand('group.daystart_power') %}
            {% set id = s.object_id.split('_power_daystart')[0] %}
           {{states('sensor.' ~ id ~ '_actueel')}}
        {%- endfor %}

now I need the automation to set those for each of those.
thanks if you would

please let me tag you @123 @petro, and please don’t feel pushed…

maybe a python script would be easier, but then I am still in need for assistance, this is a bare concept:

##########################################################################################
# Python script to generate daystart sensors for usage and power, based on their
# respective sensors 'totaal' and 'actueel' sensor, using intermediary input_numbers

# @mariusthvdb 20210311
##########################################################################################

groupPower = 'group.daystart_power'

idPower = ''
sensor_valuePower = ''

for entity_id in hass.states.get(groupPower).attributes['entity_id']:
    statePower = hass.states.get(entity_id).object_id
    idPower = statePower.split('_power_daystart')[0]

    input_idPower = 'input_number.{}_power_daystart'.format(idPower);
    sensor_idPower = 'sensor.{}_actueel'.format(idPower);

##########################################################################################
# Select Input_number and set value to the corresponding sensor
##########################################################################################

# get sensor value
    sensor_valuePower = hass.states.get(sensor_idPower).state

# and assign to input_number
    hass.services.call(
        'input_number',
        'set_value',
        {'entity_id': input_idPower,
         'value': sensor_valuePower}
        )

##########################################################################################

groupUsage = 'group.daystart_usage'

idUsage = ''
sensor_valueUsage = ''

for entity_id in hass.states.get(groupUsage).attributes['entity_id']:
    stateUsage = hass.states.get(entity_id).object_id
    idUsage = stateUsage.split('_usage_daystart')[0]

    input_idUsage = 'input_number.{}_usage_daystart'.format(idUsage);
    sensor_idUsage = 'sensor.{}_totaal'.format(idUsage);

##########################################################################################
# Select Input_number and set value to the corresponding sensor
##########################################################################################

# get sensor value
    sensor_valueUsage = hass.states.get(sensor_idUsage).state

# and assign to input_number
    hass.services.call(
        'input_number',
        'set_value',
        {'entity_id': input_idUsage,
         'value': sensor_valueUsage}
        )

seems to work, though might probably be streamlined?
edit
added the sensors for usage/totaal

edit2

this is working even more directly, no more need for input_numbers and HA template sensors, simply set them directly in the python script…

##########################################################################################
# Python script to generate daystart sensors for usage and power, based on their
# respective sensors 'totaal' and 'actueel' sensor

# @mariusthvdb 20210311
##########################################################################################

group = 'group.z_wave_switches'

for entity_id in hass.states.get(group).attributes['entity_id']:
    sensorName = hass.states.get(entity_id).object_id

    sensor_idPower = 'sensor.{}_actueel'.format(sensorName);
    sensor_idUsage = 'sensor.{}_totaal'.format(sensorName);

##########################################################################################
# Select Input_number and set value to the corresponding sensor
##########################################################################################

# get sensor value
    valuePower = hass.states.get(sensor_idPower).state
    valueUsage = hass.states.get(sensor_idUsage).state

    sensorPower = 'sensor.{}_power_daystart'.format(sensorName);
    sensorUsage = 'sensor.{}_usage_daystart'.format(sensorName);

# and set sensors '_daystart' directly with correct value
# no more use of intermediary input_numbers and template sensors!

    hass.states.set(sensorPower, valuePower,
        {'unit_of_measurement': 'Watt'
        })

    hass.states.set(sensorUsage, valueUsage,
        {'unit_of_measurement': 'kWh'
        })