This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
lysdal's profile picture

CCSensors Information about a IC2 EU Storage Unit

Started by lysdal, 04 August 2012 - 07:08 PM
lysdal #1
Posted 04 August 2012 - 09:08 PM
We've tried everything now. So im gonna try asking here.
We are trying to make a CCSensor that will give information to a monitor about a EU Storage Unit (Batbox)
It gives a error at the
write("Energy : "..Energy.."/"..Max.." ")
line

term.clear()
term.setCursorPos(1,1)
sensor = sensors.getReadingAsTable("right", "EUSensor","maxStorage" ,"energy" ,"tier")
Max = sensor[1]
Energy = sensor[2]
Tier = sensor[3]
if Tier == 1 then
unit = "BatBox"
elseif Tier == 2 then
unit = "MFE"
elseif Tier == 3 then
unit = "MFSU"
end
while true do
sleep(0)
term.clear()
term.setCursorPos(1,1)
write("================UTILITIES=================")
term.setCursorPos(1,2)
write("Time		  : "..textutils.formatTime(os.time(),true))
term.setCursorPos(1,4)
write("===============STORAGE UNIT===============")
term.setCursorPos(1,5)
write("Energy		: "..Energy.."/"..Max.." ")
term.setCursorPos(1,6)
write("Tier		  : "..unit.." ")
end
I just cant get it to work :P/>/>
KFAFSP #2
Posted 04 August 2012 - 09:22 PM
Maybe you have a nil Value in Energy or Max. Check that. Or give us the error Message :P/>/>
And, if I remember correctly, dont you need to index the dictionary table returned by getReadingAsTable(…) ?
But I am not so sure about that, just check the Variable contents.
lysdal #3
Posted 04 August 2012 - 09:26 PM
The error is

gui:29: attempt to concatenate nil and string
How would i index the table returned?
lysdal #4
Posted 04 August 2012 - 09:42 PM
I need help D:
KFAFSP #5
Posted 05 August 2012 - 12:45 AM
Ok. That is what I expected.


local Reading = sensors.getSensorReadingAsDict(<SIDE>,<SENSOR>,<TARGET>,<PROBE>);


should give you a Dictionary of all Sensor Readings.

You may then get every reading printed, by looping through :


for Name, Value in pairs(Reading) do
  print(Name, Value)
end

EDIT :

If you just want e.g. "tier", then you acces it like that :


local Tier = Reading.tier
-- OR
local Tier = Reading.["tier"]

But you need to be sure, the Key to the Value is called "tier".

I just made a test.

You need to get all Targets first!

In your case it should be:

Targets = sensors.getAvailableTargetsforProbe("right", <NAME of your sensor (EUSensor?)>, "EUStorage")

if #Targets ~= 0 then
  Reading = sensors.getReadingAsTable("right", <NAME of your sensor (EUSensor?)>, Target[1], "EUStorage")

  Max = Reading.maxStorage
  Energy = Reading.energy
  Tier = Reading.tier
else
  Max = 0
  Energy = 0
  Tier = 1
end
lysdal #6
Posted 05 August 2012 - 09:11 AM
Could you give me the complete code? :P/>/>
because

term.clear()
term.setCursorPos(1,1)
Targets = sensors.getAvailableTargetsforProbe("right", "EUSensor", "EUStorage")
if #Targets ~= 0 then
  Reading = sensors.getReadingAsTable("right", "EUSensor", Target[1], "EUStorage")
  Max = Reading.maxStorage
  Energy = Reading.energy
  Tier = Reading.tier
else
  Max = 0
  Energy = 0
  Tier = 1
end
if Tier == 1 then
unit = "BatBox"
elseif Tier == 2 then
unit = "MFE"
elseif Tier == 3 then
unit = "MFSU"
end
while true do
sleep(0)
term.clear()
term.setCursorPos(1,1)
write("================UTILITIES=================")
term.setCursorPos(1,2)
write("Time	: "..textutils.formatTime(os.time(),true))
term.setCursorPos(1,4)
write("===============STORAGE UNIT===============")
term.setCursorPos(1,5)
write("Energy	 : "..Energy.."/"..Max.." ")
term.setCursorPos(1,6)
write("Tier	: "..unit.." ")
end
Dosent work
It gives the error

gui:7: attempt to index ? (a nil value)
lysdal #7
Posted 05 August 2012 - 01:36 PM
bump
KFAFSP #8
Posted 05 August 2012 - 02:52 PM
Oh, Spelling ERROR! Sorry…

Target[1] -> Targets[1]


Targets = sensors.getAvailableTargetsforProbe("right", "EUSensor", "EUStorage")
if #Targets ~= 0 then
  Reading = sensors.getReadingAsTable("right", "EUSensor", Targets[1], "EUStorage")
  Max = Reading.maxStorage
  Energy = Reading.energy
  Tier = Reading.tier
else
  Max = 0
  Energy = 0
  Tier = 1
end
lysdal #9
Posted 05 August 2012 - 03:03 PM
Error D:

gui:36: attempt to concatenate nil and string
thats the

write("Energy	 : "..Energy.."/"..Max.." ")
line.
So it still have a problem reading the EU Storage Unit D:
lysdal #10
Posted 05 August 2012 - 04:26 PM
bump
KFAFSP #11
Posted 05 August 2012 - 05:44 PM
Ok, then go debugging by yourself. Try finding the error by doing stuff like

print(Energy)
print(Max)
If you dont get any Values, it means the Readings Table is Empty. Because the Indexes are correct, I checked that twice.
Try adding this after my If-Clause:

for k, v in pairs(Reading) do
  print(k, v)
end
So we can get all readings printed out. I think the error is somewhere else, maybe the Hardware setup? There can only be a nil error, when there is a valid storage found, because of the if-clause. So, maybe, we got the wrong Probe? Just give me the prints please.
lysdal #12
Posted 05 August 2012 - 06:18 PM

term.setCursorPos(1,5)
print(Energy)
term.setCursorPos(1,6)
print(Max)
Prints

=
But!

for k, v in pairs(Reading) do
  print(k, v)
end
Outputs some information
But not in the way we want it
It outputs Name position and type not energy or anything.

1name
2ic.common.TileEntityElectricBatBox@1986a8
3type
4TileEntityElectricBatBox
5xCoord
6323
7yCoord
868
9zCoord
1077
lysdal #13
Posted 05 August 2012 - 08:29 PM
bump
KFAFSP #14
Posted 05 August 2012 - 09:15 PM
That explains a lot… I'm not very familiar with ccSensors, but this print looks like the print of the Target-Information table! And that means :

We used an erroneous Probe Name!

Finally, this seems to be the answer. I made a test Setup, and your Program works! :P/>/> Change line :

Reading = sensors.getReadingAsTable("right", "EUSensor", Target[1], "EUStorage")
To :

Reading = sensors.getSensorReadingAsDict("right", "EUSensor", Targets[1], "EUStorage")

We never got the actual Reading, because we always got the target info… D: But this works!

My complete tested Code:

term.clear()
term.setCursorPos(1,1)
Targets = sensors.getAvailableTargetsforProbe("right", "EUSensor", "EUStorage")
if #Targets ~= 0 then
  Reading = sensors.getSensorReadingAsDict("right", "EUSensor", Targets[1], "EUStorage")
  Max = Reading.maxStorage
  Energy = Reading.energy
  Tier = Reading.tier

  for k, v in pairs(Reading) do print(k, V) end
else
  Max = 0
  Energy = 0
  Tier = 1
end
if Tier == 1 then
unit = "BatBox"
elseif Tier == 2 then
unit = "MFE"
elseif Tier == 3 then
unit = "MFSU"
end
while true do
sleep(0)
term.clear()
term.setCursorPos(1,1)
write("================UTILITIES=================")
term.setCursorPos(1,2)
write("Time	 : "..textutils.formatTime(os.time(),true))
term.setCursorPos(1,4)
write("===============STORAGE UNIT===============")
term.setCursorPos(1,5)
write("Energy	: "..Energy.."/"..Max.." ")
term.setCursorPos(1,6)
write("Tier	 : "..unit.." ")
end
KFAFSP #15
Posted 05 August 2012 - 09:20 PM
Proof of work at bottom.

I hope this helped, even if it was kind of a long wait :P/>/>
If you are not comfortable with this solution, I could help you code a more error-safe version.
Just PM me, because now I'm into ccSensors, i got the ropes.
Neywiny #16
Posted 05 July 2013 - 11:38 AM
Might be a bit late here, but the code didn't work. I tried it and it said (programnamehere):33: attempt to concatenate nil and string.
The time still works, and the utilities and storage unit test is still there, but it will not show the energy.
zane20062 #17
Posted 19 December 2013 - 09:20 PM
Im trying the same thing! Your codes great but it just wont show the energy!
I tinkered around abit and i got no errors but it didnt print anything.
I also got nill value errors!
Someone please help!
Lyqyd #18
Posted 19 December 2013 - 10:56 PM
Please post your own topic with the code you're using and the mods you're using, including version numbers. This topic is ancient.