Write an inputdatetime sensor to another

service: input_datetime.set_datetime
target:
  entity_id: >-
    input_datetime.electriciteit_maandpiek_datum_{{
    now()|as_timestamp|timestamp_custom('%B')|replace('y','i')|replace('rch','art')|replace('ai','ei')|replace('ne','ni')|replace('st','stus')|replace('Oc','ok')|lower
    }}
data:
  value: "{{(states('sensor.time_formatted')) |string}}"

here is my problem
value: “{{(states(‘sensor.time_formatted’)) |string}}”

I made an automation needs to write every end of the month a value to an input_datetime
but I don’t know how to write it
sensor.time_formatted looks like this : 21 januari 09:15-09:30

action:
  - service: input_datetime.set_datetime
    target:
      entity_id: >-
        input_datetime.electriciteit_maandpiek_datum_{{
        now()|as_timestamp|timestamp_custom('%B')|replace('y','i')|replace('rch','art')|replace('ai','ei')|replace('ne','ni')|replace('st','stus')|replace('Oc','ok')|lower
        }}
    data:
      datetime: "{{(states('sensor.time_formatted'))}}"

gives me “Invalid datetime specified: 02 februari 08:30-08:45 for dictionary value @ data[‘datetime’]”

1 Like

How should a time span be displayed?

The value of an Input Datetime helper can be a time, date, or both. It cannot be a time range like what you want to do.

You can store that value in an Input Text.

like this
02 februari 08:30-08:45

it just has to be copy paste. it is the quarter in a month that has the highest energy use. that’s important in belgium. Each month it resets. so at the end of every month I want it copied to another entity ( helper), so I can see the difference each month

so if I change my input_datetime helpers to input_text it will work ?

like this ?

service: input_text.set_value
target:
  entity_id: >-
    input_text.electriciteit_maandpiek_datum_{{
    now()|as_timestamp|timestamp_custom('%B')|replace('y','i')|replace('rch','art')|replace('ai','ei')|replace('ne','ni')|replace('st','stus')|replace('Oc','ok')|lower
    }}
data: "{{(states('sensor.time_formatted'))}}"

Needs value: (ref):

service: input_text.set_value
target:
  entity_id: >-
    input_text.electriciteit_maandpiek_datum_{{
    now()|as_timestamp|timestamp_custom('%B')|replace('y','i')|replace('rch','art')|replace('ai','ei')|replace('ne','ni')|replace('st','stus')|replace('Oc','ok')|lower
    }}
data:
  value: "{{ states('sensor.time_formatted') }}"

Good to see it’s working for februari :wink:

hmmm, it does not :slight_smile:
the state of februari is unavailable

No, I meant it as a ‘not possible’. See 123’s comment.

Have you created the input_text entity? Definitely spelled correctly? Template returns:

input_text.electriciteit_maandpiek_datum_februari

afbeelding

The template editor does not run the service: it simply evaluates the Jinja templates.

You need to go to the Services tab to try it out outside of your automation.

image

so you mean the code has to work ?

I’m not sure how to answer that. Yes, you need to run the input_text.set_value service in order to populate your text helper.

The code above (starting service:) is YAML configuration for part of a Script or Automation action that will do that. That YAML contains a couple of Jinja templates (the bits in {{...}}) to get the name of the month’s text entity and the time you want to record.

The Template Editor only deals with the Jinja aspects of the input. Your screenshot from the Template Editor above shows it inserting the outputs from the two Jinja templates into the YAML, showing what the resultant code will look like at this point in time when you run it in a script or automation. The Template Editor does not actually do anything though: it’s just a useful tool for getting your Jinja templates doing what you want before you use them in your configuration.

Ensure you have created twelve Input Text helpers, one for each month of the year.

input_text.electriciteit_maandpiek_datum_januari
input_text.electriciteit_maandpiek_datum_februari
input_text.electriciteit_maandpiek_datum_maart
input_text.electriciteit_maandpiek_datum_april
input_text.electriciteit_maandpiek_datum_mei
input_text.electriciteit_maandpiek_datum_juni
input_text.electriciteit_maandpiek_datum_juli
input_text.electriciteit_maandpiek_datum_augustus
input_text.electriciteit_maandpiek_datum_september
input_text.electriciteit_maandpiek_datum_oktober
input_text.electriciteit_maandpiek_datum_november
input_text.electriciteit_maandpiek_datum_december

Then you can use this version of the service call to store the value of sensor.time_formatted in the current month’s Input Text helper.

service: input_text.set_value
target:
  data: "{{ states('sensor.time_formatted') }}"
  entity_id: >-
    input_text.electriciteit_maandpiek_datum_{{
      ['januari', 'februari', 'maart', 'april', 'mei',
      'juni', 'juli', 'augustus', 'september',
      'oktober', 'november', 'december'][now().month - 1] }}

1 Like

Guys,

thank you all for helping me

1 Like