Put date and time into json string

hello everyone,

I’am an rookie on home assistant, i have used before domoticz.
after i have set up different things i run on an little problem.

I want to read out some values about the fronius symo inverter.
the valuas are DC voltages and current of the mptt1 and mptt2
This is possible with an json string with an time stamp in it.
my problem is how can i set automaticly the current datetime into the string what i send.

my example of the string is:

http://192.168.2.15/solar_api/v1/GetArchiveData.cgi?Scope=System&StartDate=2024-11-28T08:12:00+00:04&EndDate=2024-11-28T08:12:14Z&Channel=Current_DC_String_1&Channel=Current_DC_String_2&Channel=Voltage_DC_String_1&Channel=Voltage_DC_String_2

i have done some research but i can get it find or don’t understand it perfectly.

i hope that anyone can give me the answer, thanks.

I’m a bit confused what time zone you’re in that is UTC+4 minutes…

2 Likes

thanks, for this info.
and yes the timezone was not correct, because i have done some testing. the correct timezone is +1 (Netherlands).

i will try it, my inverter go in sleep at night and daytime i will work.

but thanks for this;)

I get now an other point, howe can i put in the datetime string{{ (now() …}} into the string.
I have deleted the dates and times and put in the datetime string{{ (now() …}} but that is not working.

see example:

http://192.168.2.15/solar_api/v1/GetArchiveData.cgi?Scope=System&StartDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}&EndDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}Z&Channel=Voltage_DC_String_1

can annyone help me with this, i think that i must make template etc but i can’t find it.
I’am still learning:)

thanks.

platform: rest
      resource: http://192.168.2.15/solar_api/v1/GetArchiveData.cgi?Scope=System&StartDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}&EndDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}Z&Channel=Voltage_DC_String_1
      name: fronius_voltage_DC_String_1
      value_template: '{{ value_json.Body["Data"]["inverter/1"]["Data"]["Voltage_DC_String_1"]["Values"]["0"] }}'
      scan_interval: 300
      unit_of_measurement: V

Use resource_template (docs):

image

The json string is working (thanks for the help) only extracting the right valua will be an problem now.

I need the values of “192” and that is for the “Voltage_DC_String_1” 454.90 volts.

“192” is changes, and this is not important (this will give the seconds 0 to 300) and i do an 5min readout.

My quastion is there an possibillity to give in my command that this can be everything. I have markt the field with “???”

my config:

platform: rest
      resource_template: http://192.168.2.15/solar_api/v1/GetArchiveData.cgi?Scope=System&StartDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}&EndDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}Z&Channel=Current_DC_String_1&Channel=Current_DC_String_2&Channel=Voltage_DC_String_1&Channel=Voltage_DC_String_2
      name: fronius_Current_DC_String_1
      value_template: '{{ value_json.Body["Data"]["inverter/1"]["Data"]["Voltage_DC_String_1"]["Values"]["?????"] }}'
      scan_interval: 60
      unit_of_measurement: V

responce:

{
	"Body" : 
	{
		"Data" : 
		{
			"inverter/1" : 
			{
				"Data" : 
				{
					"Current_DC_String_1" : 
					{
						"Unit" : "A",
						"Values" : 
						{
							"192" : 0
						},
						"_comment" : "channelId=66050"
					},
					"Current_DC_String_2" : 
					{
						"Unit" : "A",
						"Values" : 
						{
							"192" : 0
						},
						"_comment" : "channelId=131586"
					},
					"Voltage_DC_String_1" : 
					{
						"Unit" : "V",
						"Values" : 
						{
							"192" : 454.90000000000003
						},
						"_comment" : "channelId=66049"
					},
					"Voltage_DC_String_2" : 
					{
						"Unit" : "V",
						"Values" : 
						{
							"192" : 455.90000000000003
						},
						"_comment" : "channelId=131585"
					}

To be clear — you want to read the 454.9, but the 192 can change and you don’t care what it is? Perhaps:

value_template: '{{ value_json["Body"]["Data"]["inverter/1"]["Data"]["Voltage_DC_String_1"]["Values"].values()|first }}'

That will read the value of the first item in that dictionary regardless of what the key is.

I have tried but it is not working.

answer on your quastion:
“To be clear — you want to read the 454.9, but the 192 can change and you don’t care what it is?”
Yes.

I have changed my script and do an check on “dev.tools” and than i get the next warning:

Configuration warnings

Invalid config for 'rest' from integration 'sensor' at packages/fronius_symo.yaml, line 56: invalid template (TemplateSyntaxError: expected name or number) for dictionary value 'value_template', got '{{ value_json.["Body"]["Data"]["inverter/1"]["Data"]["Voltage_DC_String_1"]["Values"].values()|first }}'

Did you what is wrong with it.

You’ve left a dot in between value_json and ["Body"].

...got '{{ value_json.["Body"]["Data"]...
---------------------^

My test (needed to add five }s to the end of your JSON to complete it):

Template is:

{% set value_json = {"Body":{"Data":{"inverter/1":{"Data":{"Current_DC_String_1":{"Unit":"A","Values":{"192":0},"_comment":"channelId=66050"},"Current_DC_String_2":{"Unit":"A","Values":{"192":0},"_comment":"channelId=131586"},"Voltage_DC_String_1":{"Unit":"V","Values":{"192":454.90000000000003},"_comment":"channelId=66049"},"Voltage_DC_String_2":{"Unit":"V","Values":{"192":455.90000000000003},"_comment":"channelId=131585"}}}}}} %}
{{ value_json["Body"]["Data"]["inverter/1"]["Data"]["Voltage_DC_String_1"]["Values"].values()|first }}

I have tried something and it is now working :slight_smile:

see the config:

value_template: '{{ value_json.Body ["Data"]["inverter/1"]["Data"]["Voltage_DC_String_1"]["Values"].values()|first }}'

this is also working:

value_template: '{{ value_json ["Body"]["Data"]["inverter/1"]["Data"]["Voltage_DC_String_1"]["Values"].values()|first }}'

thanks for your help, perfect

I have done some clean up and making the config little smaler.
This is working but i don’t know of it is right way, but it is working :slight_smile:

For any one that have an “Fronius Symo 7.0-3-M” 3-phase inverter is this maybe helpfull, i hope.

config:

rest:
    - resource: http://192.168.2.15/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=3PInverterData
      sensor: 
      - name: "fronius_IAC_L1"
        value_template: '{{ value_json.Body["Data"]["IAC_L1"]["Value"] }}'
        unit_of_measurement: "A"
      - name: "fronius_IAC_L2"
        value_template: '{{ value_json.Body["Data"]["IAC_L2"]["Value"] }}'
        unit_of_measurement: "A"
      - name: "fronius_IAC_L3"
        value_template: '{{ value_json.Body["Data"]["IAC_L3"]["Value"] }}'
        unit_of_measurement: "A"
      - name: "fronius_UAC_L1"
        value_template: '{{ value_json.Body["Data"]["UAC_L1"]["Value"] }}'
        unit_of_measurement: "V"
      - name: "fronius_UAC_L2"
        value_template: '{{ value_json.Body["Data"]["UAC_L2"]["Value"] }}'
        unit_of_measurement: "V"
      - name: "fronius_UAC_L3"
        value_template: '{{ value_json.Body["Data"]["UAC_L3"]["Value"] }}'
        unit_of_measurement: "V"


    - resource_template: http://192.168.2.15/solar_api/v1/GetArchiveData.cgi?Scope=System&StartDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}&EndDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}Z&Channel=Current_DC_String_1&Channel=Current_DC_String_2&Channel=Voltage_DC_String_1&Channel=Voltage_DC_String_2
      sensor:
      - name: "fronius_Voltage_DC_String_1"
        value_template: '{{ value_json ["Body"]["Data"]["inverter/1"]["Data"]["Voltage_DC_String_1"]["Values"].values()|first }}'
        unit_of_measurement: "V"
      - name: "fronius_Voltage_DC_String_2"
        value_template: '{{ value_json ["Body"]["Data"]["inverter/1"]["Data"]["Voltage_DC_String_2"]["Values"].values()|first }}'
        unit_of_measurement: "V"
      - name: "fronius_Current_DC_String_1"
        value_template: '{{ value_json ["Body"]["Data"]["inverter/1"]["Data"]["Current_DC_String_1"]["Values"].values()|first }}'
        unit_of_measurement: "A"
      - name: "fronius_Current_DC_String_2"
        value_template: '{{ value_json ["Body"]["Data"]["inverter/1"]["Data"]["Current_DC_String_2"]["Values"].values()|first }}'
        unit_of_measurement: "A"

That all looks fine. There are two common formats to refer to objects within a dictionary. For:

"example": {"foo": "bar"}

you can use example.foo (“dot notation”) or example['foo'] (“bracket notation”).

You’ve used a bit of a mixture and also put some spaces in which still works, but I wouldn’t recommend it. Bracket notation is generally better, and is the only way to access items that start with a number, contain weird characters or are reserved Python keywords.

If you add device_class: current or device_class: voltage, you’ll get suitable icons for those entities.

1 Like

Thanks a lot for your work! I have adapted the code for my use case and added the power values for AC L1-L3. I also calculated the power values for MPP 1 and MPP 2 with the help of ChatGPT. I can barely understand the code.

I added to configuration.yaml:
rest:!include rest_sensors.yaml

and added a new rest_sensors.yaml file with:

  - resource: http://192.168.178.22/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=3PInverterData
    sensor:
      # AC currents
      - name: "fronius_IAC_L1"
        value_template: "{{ value_json['Body']['Data']['IAC_L1']['Value'] }}"
        unit_of_measurement: "A"
        device_class: current
      - name: "fronius_IAC_L2"
        value_template: "{{ value_json['Body']['Data']['IAC_L2']['Value'] }}"
        unit_of_measurement: "A"
        device_class: current
      - name: "fronius_IAC_L3"
        value_template: "{{ value_json['Body']['Data']['IAC_L3']['Value'] }}"
        unit_of_measurement: "A"
        device_class: current
        
      # AC voltages
      - name: "fronius_UAC_L1"
        value_template: "{{ value_json['Body']['Data']['UAC_L1']['Value'] }}"
        unit_of_measurement: "V"
        device_class: voltage
      - name: "fronius_UAC_L2"
        value_template: "{{ value_json['Body']['Data']['UAC_L2']['Value'] }}"
        unit_of_measurement: "V"
        device_class: voltage
      - name: "fronius_UAC_L3"
        value_template: "{{ value_json['Body']['Data']['UAC_L3']['Value'] }}"
        unit_of_measurement: "V"
        device_class: voltage

      # Calculated AC power per phase (P = U * I)
      - name: "fronius_PAC_L1"
        value_template: "{{ states('sensor.fronius_IAC_L1') | float * states('sensor.fronius_UAC_L1') | float }}"
        unit_of_measurement: "W"
        device_class: power
      - name: "fronius_PAC_L2"
        value_template: "{{ states('sensor.fronius_IAC_L2') | float * states('sensor.fronius_UAC_L2') | float }}"
        unit_of_measurement: "W"
        device_class: power
      - name: "fronius_PAC_L3"
        value_template: "{{ states('sensor.fronius_IAC_L3') | float * states('sensor.fronius_UAC_L3') | float }}"
        unit_of_measurement: "W"
        device_class: power

  - resource-template: http://192.168.178.22/solar_api/v1/GetArchiveData.cgi?Scope=System&StartDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}&EndDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}Z&Channel=Current_DC_String_1&Channel=Current_DC_String_2&Channel=Voltage_DC_String_1&Channel=Voltage_DC_String_2
    sensor:
      # DC voltages
      - name: "fronius_Voltage_DC_String_1"
        value_template: "{{ value_json['Body']['Data']['inverter/1']['Data']['Voltage_DC_String_1']['Values'].values() | first }}"
        unit_of_measurement: "V"
        device_class: voltage
      - name: "fronius_Voltage_DC_String_2"
        value_template: "{{ value_json['Body']['Data']['inverter/1']['Data']['Voltage_DC_String_2']['Values'].values() | first }}"
        unit_of_measurement: "V"
        device_class: voltage

      # DC currents
      - name: "fronius_Current_DC_String_1"
        value_template: "{{ value_json['Body']['Data']['inverter/1']['Data']['Current_DC_String_1']['Values'].values() | first }}"
        unit_of_measurement: "A"
        device_class: current
      - name: "fronius_Current_DC_String_2"
        value_template: "{{ value_json['Body']['Data']['inverter/1']['Data']['Current_DC_String_2']['Values'].values() | first }}"
        unit_of_measurement: "A"
        device_class: current

      # Calculated DC power per MPP string (P = U * I)
      - name: "fronius_PDC_String_1"
        value_template: "{{ (value_json.Body.Data['inverter/1'].Data.Voltage_DC_String_1.Values.values() | first | float) * (value_json.Body.Data['inverter/1'].Data.Current_DC_String_1.Values.values() | first | float) }}"
        unit_of_measurement: "W"
        device_class: power
      - name: "fronius_PDC_String_2"
        value_template: "{{ (value_json.Body.Data['inverter/1'].Data.Voltage_DC_String_2.Values.values() | first | float) * (value_json.Body.Data['inverter/1'].Data.Current_DC_String_2.Values.values() | first | float) }}"
        unit_of_measurement: "W"
        device_class: power

now I added L1 - L3 from Powermeter:

  - resource: http://192.168.178.22/solar_api/v1/GetInverterRealtimeData.cgi?Scope=Device&DeviceId=1&DataCollection=3PInverterData
    sensor:
      # AC currents
      - name: "fronius_IAC_L1"
        value_template: "{{ value_json['Body']['Data']['IAC_L1']['Value'] }}"
        unit_of_measurement: "A"
        device_class: current
      - name: "fronius_IAC_L2"
        value_template: "{{ value_json['Body']['Data']['IAC_L2']['Value'] }}"
        unit_of_measurement: "A"
        device_class: current
      - name: "fronius_IAC_L3"
        value_template: "{{ value_json['Body']['Data']['IAC_L3']['Value'] }}"
        unit_of_measurement: "A"
        device_class: current
        
      # AC voltages
      - name: "fronius_UAC_L1"
        value_template: "{{ value_json['Body']['Data']['UAC_L1']['Value'] }}"
        unit_of_measurement: "V"
        device_class: voltage
      - name: "fronius_UAC_L2"
        value_template: "{{ value_json['Body']['Data']['UAC_L2']['Value'] }}"
        unit_of_measurement: "V"
        device_class: voltage
      - name: "fronius_UAC_L3"
        value_template: "{{ value_json['Body']['Data']['UAC_L3']['Value'] }}"
        unit_of_measurement: "V"
        device_class: voltage

      # Calculated AC power per phase (P = U * I)
      - name: "fronius_PAC_L1"
        value_template: "{{ states('sensor.fronius_IAC_L1') | float * states('sensor.fronius_UAC_L1') | float }}"
        unit_of_measurement: "W"
        device_class: power
      - name: "fronius_PAC_L2"
        value_template: "{{ states('sensor.fronius_IAC_L2') | float * states('sensor.fronius_UAC_L2') | float }}"
        unit_of_measurement: "W"
        device_class: power
      - name: "fronius_PAC_L3"
        value_template: "{{ states('sensor.fronius_IAC_L3') | float * states('sensor.fronius_UAC_L3') | float }}"
        unit_of_measurement: "W"
        device_class: power

  - resource_template: http://192.168.178.22/solar_api/v1/GetArchiveData.cgi?Scope=System&StartDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}&EndDate={{ (now() + timedelta(minutes=-5)).strftime('%Y-%m-%dT%H:%M:%S+01:00') }}Z&Channel=Current_DC_String_1&Channel=Current_DC_String_2&Channel=Voltage_DC_String_1&Channel=Voltage_DC_String_2
    sensor:
      # DC voltages
      - name: "fronius_Voltage_DC_String_1"
        value_template: "{{ value_json['Body']['Data']['inverter/1']['Data']['Voltage_DC_String_1']['Values'].values() | first }}"
        unit_of_measurement: "V"
        device_class: voltage
      - name: "fronius_Voltage_DC_String_2"
        value_template: "{{ value_json['Body']['Data']['inverter/1']['Data']['Voltage_DC_String_2']['Values'].values() | first }}"
        unit_of_measurement: "V"
        device_class: voltage

      # DC currents
      - name: "fronius_Current_DC_String_1"
        value_template: "{{ value_json['Body']['Data']['inverter/1']['Data']['Current_DC_String_1']['Values'].values() | first }}"
        unit_of_measurement: "A"
        device_class: current
      - name: "fronius_Current_DC_String_2"
        value_template: "{{ value_json['Body']['Data']['inverter/1']['Data']['Current_DC_String_2']['Values'].values() | first }}"
        unit_of_measurement: "A"
        device_class: current

      # Calculated DC power per MPP string (P = U * I)
      - name: "fronius_PDC_String_1"
        value_template: "{{ (value_json.Body.Data['inverter/1'].Data.Voltage_DC_String_1.Values.values() | first | float) * (value_json.Body.Data['inverter/1'].Data.Current_DC_String_1.Values.values() | first | float) }}"
        unit_of_measurement: "W"
        device_class: power
      - name: "fronius_PDC_String_2"
        value_template: "{{ (value_json.Body.Data['inverter/1'].Data.Voltage_DC_String_2.Values.values() | first | float) * (value_json.Body.Data['inverter/1'].Data.Current_DC_String_2.Values.values() | first | float) }}"
        unit_of_measurement: "W"
        device_class: power

  - resource: http://192.168.178.22/solar_api/v1/GetMeterRealtimeData.cgi?Scope=Device&DeviceId=0
    sensor:
      # Active power per phase (W)
      - name: "fronius_meter_power_l1"
        value_template: "{{ value_json['Body']['Data']['PowerReal_P_Phase_1'] }}"
        unit_of_measurement: "W"
        device_class: power
      - name: "fronius_meter_power_l2"
        value_template: "{{ value_json['Body']['Data']['PowerReal_P_Phase_2'] }}"
        unit_of_measurement: "W"
        device_class: power
      - name: "fronius_meter_power_l3"
        value_template: "{{ value_json['Body']['Data']['PowerReal_P_Phase_3'] }}"
        unit_of_measurement: "W"
        device_class: power

      # Total active power (W)
      - name: "fronius_meter_power_total"
        value_template: "{{ value_json['Body']['Data']['PowerReal_P_Sum'] }}"
        unit_of_measurement: "W"
        device_class: power

      # Voltage per phase (V)
      - name: "fronius_meter_voltage_l1"
        value_template: "{{ value_json['Body']['Data']['Voltage_AC_Phase_1'] }}"
        unit_of_measurement: "V"
        device_class: voltage
      - name: "fronius_meter_voltage_l2"
        value_template: "{{ value_json['Body']['Data']['Voltage_AC_Phase_2'] }}"
        unit_of_measurement: "V"
        device_class: voltage
      - name: "fronius_meter_voltage_l3"
        value_template: "{{ value_json['Body']['Data']['Voltage_AC_Phase_3'] }}"
        unit_of_measurement: "V"
        device_class: voltage

      # Current per phase (A)
      - name: "fronius_meter_current_l1"
        value_template: "{{ value_json['Body']['Data']['Current_AC_Phase_1'] }}"
        unit_of_measurement: "A"
        device_class: current
      - name: "fronius_meter_current_l2"
        value_template: "{{ value_json['Body']['Data']['Current_AC_Phase_2'] }}"
        unit_of_measurement: "A"
        device_class: current
      - name: "fronius_meter_current_l3"
        value_template: "{{ value_json['Body']['Data']['Current_AC_Phase_3'] }}"
        unit_of_measurement: "A"
        device_class: current