Timestamp to time

I want it to be used as a trigger in an automation to start the charger, if the car is connected.

When changing to english in my settings for HA, I get the correct format, though the issue remains.

You probably don’t need a helper in this case, as you already have the value in your original sensor and you can use that in your automation…

But let’s leave this for a bit later… Share how this sensor is created and we might be able to help better.

I did, a few posts above.

I really appriciate all of you taking the time to help.

This sensor is probably already in the format you need… You see in you language in the UI, but in the back is using proper format.

Could you share a screenshot of this sensor in Developer Tools > States?

This is Developer Tools > Templates…
Probably will be easier to visualise if you share Developer Tools > States (filtering to that sensor you created).

But I can already see you have the right format (or almost)

Look, I have to go to sleep now, but I promise I will take a look at this if no one else helps you tonight…

So, now I could test your sensor and can confirm it return a valid timestamp, so you can use it as it is in your automation, without the need to store in a input helper, or you can still save to your input helper, but in this case you don’t have to do any transformation.

It is as simple as this:

service: input_datetime.set_datetime
data:
  datetime: "{{ states('sensor.sensor_ev_cheapest_hours_start') }}"
target:
  entity_id: input_datetime.customvirtual_ev_starttid_for_smartladdning

But again, you are using an automation to take a value from a sensor, transfer that value to an input helper without any transformation, so you will use the input helper as an input in another automation, right? Why don’t you simplify this by just using your original sensor (sensor.sensor_ev_cheapest_hours_start) in the final automation, with no need for a helper?

Take a look at this example using sensor on the documentation. It requires the sensor to have state_class: timestamp, which is your case:

So your automation will be something like this:

automation:
  - trigger:
      - platform: time
        at: sensor.sensor_ev_cheapest_hours_start
    action:
      - service: switch.turn_on
        target:
          entity_id: switch.car_charger

Based on the investigation performed by EdwardTFN, it appears you don’t need a workaround. You have a Template Sensor configured as a timestamp sensor. You should be able to use it directly in a Time Trigger (see EdwardTFN’s example).

For future reference, when you tell people the value of an entity, report the value you see in Developer Tools > States. That’s the entity’s actual state value. What you see displayed elsewhere in the UI might be reformatted (in your native language or for cosmetic purposes).

1 Like

I am sorry for the late reply, I was not able to sit down with this yesterday. The reason for me to store it is to make sure the charger starts even if the Nordpool integration goes down.

Your code works, which is great, but it returns the original time, which is one hour earlier than I need. I can find a work-around for that, but it would be nice to have it correct from the start. Maybe that’s not possible?

Ah, I misunderstood it then.

I’m not sure if I understood when you said “original time”. When I see your screenshot I see the time is shown on UTC, but that shouldn’t be an issue as HA will handle the timezone for you and will show on the front end with your timezone.

I’m on GMT+1. The time returned from the sensor is correct, but th time stored in the helper is one hour of.

Skärmklipp

Could you please share the screenshot of Developer tools > States for those two entities:

  1. input_datetime.customvirtual_ev_starttid_for_smartladdning
  2. sensor.sensor_ev_cheapest_hours_start

You have a good point…


The sensor and input_datetime seems right, but in the cards they show one hour apart. I guess it is because none is GMT, but the card reports GMT for the sensor.

SkärmklippStart1
SkärmklippStart2

Ok, then please change your automation in order to call your service like this:

service: input_datetime.set_datetime
data:
  timestamp: "{{ states('sensor_ev_cheapest_hours_start') | as_timestamp }}"
target:
  entity_id: input_datetime.customvirtual_ev_starttid_for_smartladdning

Then please let me know the results.

In addition to that, but not related to the problem your are reporting now, I would suggest to change your input helper type from “Time” to “Date and time”, so you prevent another issue when displaying the cheapest time when it is after midnight.

In this case, the entity doesn’t change at all, but stays at its current value no matter how the sensor changes.

I managed to solve it by finding a code to add seconds to the helper. It is not an ideal solution, but it will do the job so that I can move on.

I’ll have to come back to it later. I cannot live with those ugly fixes for too long. :slight_smile:

trigger:
  - platform: state
    entity_id:
      - input_number.customvirtual_ev_antal_timmar_att_ladda
  - platform: state
    entity_id:
      - binary_sensor.easee_charger_online
    to: "on"
condition: []
action:
  - service: input_datetime.set_datetime
    data:
      datetime: "{{ states('sensor.sensor_ev_cheapest_hours_start') }}"
    target:
      entity_id: input_datetime.customvirtual_ev_starttid_for_smartladdning
  - delay:
      hours: 0
      minutes: 0
      seconds: 0
      milliseconds: 50
  - service: input_datetime.set_datetime
    data:
      time: >-
        {{
        (((state_attr('input_datetime.customvirtual_ev_starttid_for_smartladdning'
        , 'timestamp')) + 3600) | timestamp_custom('%H:%M', false)) }}
    target:
      entity_id: input_datetime.customvirtual_ev_starttid_for_smartladdning
mode: single