Setting Quiet Hours from HA to Victron Venus OS with MQTT Seconds to Input time helper

I’m working on an interface between Home Assistant on my RV and my Victron Venus OS running on a Pi. Much of it is coming together pretty well but I’m having a problem converting the generator Quiet Hours to and from the time helper in HA.

Victron stores the time as the number of seconds for a 24 hour period (0 to 86400) as the Quiet Time Start and Quiet Time End. I can get those values from the using MQTT.

The time values need to be in sync and only change when we drive to a new campsite and they have different quiet time hours for generator use. I typically will update the Quiet Time start and end hours in HA and the goal is to update those times in the Victron system so the generator does not start during quiet time.

  - name: "Quiet Hours Start"
    unique_id: "quiet_hours_start"
    state_topic: "victron-rv/N/device_id/settings/0/Settings/Generator0/QuietHours/StartTime"
    value_template: >
          {{ value_json.value | int(0) }}
          {% set quiet_time_start = value_json.value %}
          {% set quiet_time_start_min = ((quiet_time_start % 3600) / 60) | int %}
          {% set quiet_time_start_hour = ((quiet_time_start % 86400) / 3600) | int %}


  - name: "Quiet Hours End"
    unique_id: "quiet_hours_end"
    state_topic: "victron-rv/N/device_id/settings/0/Settings/Generator0/QuietHours/EndTime"
    value_template: >
          {{ value_json.value | int(0) }}
          {% set quiet_time_end = value_json.value %}
          {% set quiet_time_end_min = ((quiet_time_end % 3600) / 60) | int %}
          {% set quiet_time_end_hour = ((quiet_time_end % 86400) / 3600) | int %}

In the template sandbox under developer tools the numbers are close working but I’m having problems with the correct syntax to get the conversion working. I’ve imitated the json values I receive from MQTT to debug the code.

I’ve created two input time helpers in HA. I need to set these time input helpers (to HH:MM:00) when the start time (from seconds) is changed in the Victron system. And I need to convert these time helper variables (from HH:MM) to seconds and write that to to the Victron system when the time helpers are changed in HA.

{## Imitate available variables: ##}
MQTT VARIBLES (testing json received from Venus)
{% set json_quiet_time_starttime = {"max":86400,"min":0,"value":75600} %}
{% set json_quiet_time_endtime = {"max":86400,"min":0,"value":21600} %}

Quiet End Time (in seconds) is {{ json_quiet_time_endtime.value }}.
          {% set quiet_time_end = json_quiet_time_endtime.value %}
          {% set quiet_time_end_hour = ((quiet_time_end % 86400) / 3600) | int %}
          {% set quiet_time_end_min = ((quiet_time_end % 3600) / 60) |int %}
Quiet Start Time (in seconds) is {{ json_quiet_time_starttime.value }}.
          {% set quiet_time_start = json_quiet_time_starttime.value %}
          {% set quiet_time_start_hour = ((quiet_time_start % 86400) / 3600) | int %}
          {% set quiet_time_start_min = ((quiet_time_start % 3600) / 60) | int %}

Quiet End Time Hour: {{ quiet_time_end_hour }}
Quiet End Time Min: {{quiet_time_end_min}}
Quiet End Time Sec: 00
Quiet Start Time Hour: {{ quiet_time_start_hour }}
Quiet Start Time Min: {{quiet_time_start_min}}
Quiet Start Time Sec: 00

TIME HELPER VARIABLE
Quiet Time End: {{ states('input_datetime.quiet_hours_end_time_helper')  }}
Quiet Time Start: {{ states('input_datetime.quiet_hours_start_time_helper') }}
   

***RESULTS***
Result type: string
MQTT VARIBLES (testing json received from Venus)

Quiet End Time (in seconds) is 21600.
Quiet Start Time (in seconds) is 75600.

Quiet End Time Hour: 6
Quiet End Time Min: 0
Quiet Start Time Hour: 21
Quiet Start Time Min: 0

TIME HELPER VARIABLE
Quiet Time End: 07:00:00
Quiet Time Start: 21:30:00

To start, looking for help converting the Time helper variables to be in seconds from (0 to 86400). Eventually I hope to take the seconds from Victron and update the Time Helper variables.

{% set before = state_attr("binary_sensor.time_helper", "before") %}
{% set after = state_attr("binary_sensor.time_helper", "after") %}
{%set midnight = today_at("00:00:00") %}

{{ as_timestamp(before) | int - as_timestamp(midnight) | int }}
{{ as_timestamp(after) | int - as_timestamp(midnight) | int }}

I’m really missing something. Not clear where the binary sensors came from. Let me try to explain the first half of my problem a different way.

Victron Venus Generator start/stop settings:
start-stop-settings

I read the start hours using MQTT

  - name: "Quiet Hours Start"
    unique_id: "quiet_hours_start"
    state_topic: "victron-rv/N/device_id/settings/0/Settings/Generator0/QuietHours/StartTime"
    value_template: >
          {{ value_json.value | int }}

now this time is transferred to Home Assistant

{{ states('sensor.quiet_hours_start') }} 

and this sensor equals 73800 in seconds from midnight which is the same as 20:30 or (8:30 PM) I have an automation setup triggered by the change in this time.

alias: Quiet Start Time Clock Helper
description: Updates the Quiet Time Start Clock
trigger:
  - platform: state
    entity_id:
      - sensor.quiet_hours_start
condition: []
action:
  - action: input_datetime.set_datetime
    metadata: {}
    data:
      timestamp: "{{ states('sensor.quiet_hours_start') }}"
    target:
      entity_id: input_datetime.quiet_hours_start_time_helper
mode: single

When this automation runs, it competes successfully

Executed: September 8, 2024 at 1:41:32 PM
Result:
params:
  domain: input_datetime
  service: set_datetime
  service_data:
    timestamp: 73800
    entity_id:
      - input_datetime.quiet_hours_start_clock_helper
  target:
    entity_id:
      - input_datetime.quiet_hours_start_time_helper
running_script: false

The time is incorrect. It sets the time helper to 2:30 PM using the timestamp of 73800.
Quiet_hours_start

A timestamp is a daterime, really. 73800=1/1/1970 20h30 utc
What you see is likely that datetime translated to your timezone

NO, it’s the number of seconds since midnight as I mentioned in my post. It contains no date. I can even calculate the Hour and the Minute but I can’t get those numbers into the input time helper.

There is no NO. It’s the definition of a Unix timestamp (also called “epoch”) as far as your service call goes :wink:

What is your timezone ?

The Victron “time stamp” is not a Unix EPOCH time stamp. It’s just time represented by the number of seconds since midnight of any given day regardless of time zone.

I finally figured it out after days of research and this is how I ended up solving the problem It’s actually too simple compared to the the math I was trying at start of this post. I just needed to learn the language to make it work and there is no one place to learn it.

I’ve replace the device_id in the MQTT Topic for security. You need to have a bridge set up between Mosquito on HA and the Victron, Pi, cerbo, etc. and there are many tutorials to use to set this up. This is what I did:

First read the values from the Venus OS with MQTT:

  - name: "Quiet Hours Start"
    unique_id: "quiet_hours_start"
    state_topic: "victron-rv/N/device_id/settings/0/Settings/Generator0/QuietHours/StartTime"
    value_template: >
          {{ value_json.value | int }}

You now know the value reported as the Quiet Hour Start Time in seconds from midnight. In this case it’s 20:30 (9:30 pm)
RESULT
Value: {“max”:86400,“min”:0,“value”:77400}

I created a time helper called input_datetime.quiet_hours_start_clock_helper and an automation to set this helper to the time value = Quiet Hour Start Time triggered whenever the Quiet Hour Start Time changes in Venus OS

alias: Quiet Start Time Clock Helper
description: Updates the Quiet Time Start Clock
trigger:
  - platform: state
    entity_id:
      - sensor.quiet_hours_start
condition: []
action:
  - action: input_datetime.set_datetime
    metadata: {}
    data:
      time: "{{ states('sensor.quiet_hours_start') | as_timedelta }}"
    target:
      entity_id: input_datetime.quiet_hours_start_clock_helper
mode: single

What makes this work is using “{{ states(‘sensor.quiet_hours_start’) | as_timedelta }}” to set the time helper in HA.

The helper in Lovelace now displays the correct time value.
HA time Helpers

Then I wanted to do the reverse meaning when the time value of the HA time helper changes, the time is updated in Venus. To to this a second automation is needed triggered by the change of the HA time helper value and then write the new time (in seconds from midnight) to the Venus OS using MQTT.

alias: Set Venus Quiet Hours Start Time
description: ""
trigger:
  - platform: state
    entity_id:
      - input_datetime.quiet_hours_start_clock_helper
condition: []
action:
  - action: mqtt.publish
    metadata: {}
    data:
      evaluate_payload: false
      qos: 0
      retain: false
      payload: >-
        {"value" : {{
        state_attr('input_datetime.quiet_hours_start_clock_helper','timestamp')
        }} }
      topic: >-
        victron-rv/W/device_id/settings/0/Settings/Generator0/QuietHours/StartTime
mode: single

What makes this work is state_attr(‘input_datetime.quiet_hours_start_clock_helper’,‘timestamp’) to write the time to Venus OS using MQTT.

This same method is used for the end time by replacing “start” with “end” for the end time values. I hope this helps someone besides me. It was painful to figure it out over the last 3 days.

I understand that.
I was talking about the service call to set the input_datetime, whose “timestamp” attribute takes an epoch timestamp, and whatever number you pass to it will be interpreted as such.