Timestamp to time

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

In this case, go to Developer Tools, tab “Services” and then select YAML mode and paste this code over there, then press “Call service” and let me know if it runs properly (the button will be on green for a few seconds and your input helper should change) or of it fails (the button will be red for a few seconds and you should have an error message, which I’d like to know what is).

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

I just realized there is a typo there… My fault!
Please replace this line by (please adjust the Indentation accordingly):

timestamp: "{{ states('sensor.sensor_ev_cheapest_hours_start') | as_timestamp }}

I saw the typo and corrected it before, but it didn’t work anyway. I did as you said and tried in Developer tools and it worked. Now it also works in the automation. :slight_smile:

I have no idea why it didn’t work before, but thank you so much. I really appriciate it.

1 Like

I actually have another question related to this. In the sensor I am using “now().hour” as start for the search. I would also like to use an input_datetime (configured as time) as end to make sure the car is charged before leaving for work. I have tried a lot of different things to convert the hours to an int, but cannot get it to work. As I understood it, strftime is the way to go, but it seems it is just fetching parts of the string, not converting.

Is there a simple command for it?

The helpers with datetime mode have attributes for hour, minutes, etc.

Try this:

{{ state_attr('input_datetime.customvirtual_ev_starttid_for_smartladdning', 'hour') | int() }}

Thanks! This was one of the things I tried but couldn’t get to work. I must have had the syntax wrong and I probably gave up to soon. Now it works. :slight_smile:

1 Like

Is there a easy way to make a sensor that says how many hours there is left to the cheapest hour? (ex. the cheapest hour among the next 6 hours)
It would be nice to have a screen next to the dishwasher that says how many hours you should delay the start to hit that cheapest hour.

I suggest you go with @TheFes’s cheapest hour macro that provides all this information. This post is lengthy and his macro is easier to implement and you can get this information out rather easily.

1 Like