How do I "copy" a sensor value to a helper?

I have a Sensor (sensor.esp32_sdm230m_import_active_energy), a Helper (counter.at_midnight) and an Automation (… no this is not the first line of a joke!).

When the Automation fires, how can I get the Automations Action to copy the value in the sensor to the helper (so that I can later reference that saved value)?

Regards, Martin

Here is the code I use for recording the car mileage at the beginning of the month. So I copy value of sensor.audi_q5_mileage to helper input_number.month_end_mileage. In your case this might not work, however, as thre is no ‘set_value’ service for counter (you can only increase, decrease or reset its value).

  - id: 'log_car_mileage'
    alias: Log Car Mileage
    initial_state: true
      - platform: time
        at: '00:00:01'
      - condition: template
        value_template: "{{ now().day == 1 }}"
      - service: input_number.set_value
          entity_id: input_number.month_end_mileage
          value: "{{ states('sensor.audi_q5_mileage') }}"

Thank you Mirek,

I don’t think that the “counter” issue will be a problem, because (I think) I can just recreate the Helper as a number? Or is there a better data type I could use?

What is a problem for HA newbies like me though, is “code snippets” without a context. This is not a criticism of you, everybody does it. But without knowing where the code goes, I’m lost. Even if I make a guess that it is in /config/configuration.yaml, the near-impossible-to-understand yaml indentation rules makes it impractical to use snippets like this.
So if you could repost with advice on where/how to use your code,I would be most grateful.

Regards, Martin

As I understand it , the context is what you requested, an automation. You have a choice of copy-pasting it directly into automations.yaml or in configuration.yaml as explained here:

You can also use the Automation Editor. Create a new empty automation, switch it to YAML mode, copy-paste the example (although I suggest either removing the example’s first line or at least its leading hyphen) and saving it (obviously you’ll need to modify the entities to match what you have). If there are syntax errors, the Automation Editor will notify you. The Automation Editor will automatically save it to the automations.yaml file (unless you have changed the defaults).

Thank you Taras,

That is very helpful.

PS - Working through your advice, I see this:
Is this to be expected? Are there some functions/methods/routines that the UI does not know about?

I have tried something similar but it does not let me use the service .set_value for an input number with a dynamic value. I can only hardcode a value in the automation. Any ideas?

Post your code, perhaps some mistake there?

I don’t like using input_number to store what is a fixed value (eg input_number.oil_litres_yesterday set in an automation just before midnight) because if I have it displayed in a dashboard, clicking on it opens it up with the option to change it. I don’t want that in this use case!
I’m considering a Template entity that just copies the input_number but there has to be a better way!

@Wingnut What i do in such cases, is add another helper, using a sensor template based on the value of the input number…
And that sensor value is read-only, no matter how much click it :wink:

1 Like